Optional Parameters
Keywords: Optional Varible Number Parameters UDF UDS User Defined Function Subroutine Global
Question:
Is there a method of defining a UDF Function with a variable number of Parameters?
I have a LOG file routine which requires two Parameters (filename, Text) and I want to expand this to include an extra Flag Parameter for special cases but as this routine is used by hundreds of other source files I don't want to add this Parameter to all the existing code.
So I am after one Routine that Supports calls with Two or Three Parameters.
Is this possible?
Answer:
No, a WinBatch user defined function must have a dedicated number of parameters.
However here are a few work arounds:
- Define a global pointer variable and set the value as needed before calling the udfLog function, then evaluate the pointer variable from within the udfLog function.
- Leave all old calls to the udfLog as is, but in new code use a new wrapper udfLogWrap with three parameters and from within udfLogWrap call the old udfLog function.
- Modify the old udfLog code to evaluate the call stack via IntControl (77,72,0,0,0) and check whether the udfLog has been called by the udfLogWrap using three parameters or by the old way using two parameters. If applicable, then get the third parameter from the call stack.
- Concatenate the second and third parameters with a delimiter. Then in the LOG routine you can do something like:
flag = ItemExtract(2,Text,delimiter)
Text = ItemRemove(2,Text,delimiter)
- Arrays are also useful for variable number of parameters. How about passing the second parameter an array of values. Then check inside the function whether a string or an array was passed. See VarType.
If an array parse out the various optional parameters.
Article ID: W18335
Filename: Optional Parameters.txt
File Created: 2012:06:27:07:52:32
Last Updated: 2012:06:27:07:52:32