Named Optional Arguments in VBScript
QuickTest was my introduction to VBScript. I think and dream in Perl, but I am coming around to a point where I can enjoy coding in VBScript. One of my biggest VBScript annoyances early on was the inability to define optional arguments for my functions.As I said, I think in Perl, so I set about trying to find a solution similar to how I pass arguments to subs in Perl. In Perl, I like to name my arguments, so I pass an array of arguments that the sub collects into a hash as named values. The syntax looks something like this.
View as plain text
PERL:
printsentence(
'Subject' => 'The quick brown fox',
'Predicate' => 'jumps over the lazy dog.'
);
sub printsentence {
my %args = @_;
print "$args{'Subject'} $args{'Predicate'}";
}
I found an article on 4guysfromrolla.com about using optional arguments in VBScript. It suggests passing an array of arguments to the function. This is a good first step, but I don't like having to remember which value is which in the array. I still wanted my named arguments. Obviously I couldn't use a hash for this, so I used VBScript's nearest data type: a dictionary object. Once I have my parameters in a dictionary object, I can call them by name using syntax like oOptions("optionname"). The question was how to easily get all my options in a dictionary object.
I came up with a function I call DictBuild (insert platypus-related humor here). It takes an array and turns it into a dictionary object. The first value in the array is a dictionary element, the second value is that element's value, third - element, fourth - value...and so on. Basically, all even numbered values are dictionary elements and the odd value after each element name is its value.
This allows me to easily build a dictionary object with all my parameters.
View as plain text
Visual Basic:
printsentence ARRAY( _
"Subject", "The quick brown fox", _
"Predicate", "jumps over the lazy dog." _
)
Public Function printsentence(aOptions)
Set oOptions = BuildDict(aOptions)
MsgBox oOptions("Subject") & " " & oOptions("Predicate")
End Function
Now this is finally becoming something I can work with, but what about defaults for these parameters? This is where my next function comes in: GetOpts. GetOpts takes two arrays as arguments and returns a dictionary object. The first array is the default values for the function. The second array is the custom options array that was passed to the function. GetOpts starts by building a dictionary object from the defaults array. Then it overwrites the default values with the custom options array if they exist. I also wrote some discipline for myself into GetOpts. If a option isn't listed in the defaults, it won't make it into the final dictionary.
So, now my function looks like this:
View as plain text
Visual Basic:
printsentence ARRAY( _
"Subject", "The quick brown fox", _
"Predicate", "jumps over the lazy dog" _
)
Public Function printsentence(aOptions)
Set oOptions = GetOpts( ARRAY( _
"Subject", "Coke", _
"Predicate", "is it", _
"Punctuation", "!" _
), aOptions)
MsgBox oOptions("Subject") & " " & oOptions("Predicate") & _
oOptions("Punctuation")
End Function
I think that about does it for my method of using named optional arguments in VBScript functions. I should note that I only use this for optional arguments, I put the variable names directly in the function signature for required values. So one of my real function delcarations looks like
View as plain text
Visual Basic:
Public Function WebCheckBoxSelectByText ( ByRef oCheckBox, sSelectText, aOptions )
The code for BuildDict and GetOpts is here:
View as plain text
Visual Basic:
Public Function GetOpts(ByRef aDefaults, ByRef aCustom)
Set oDefaults = DictBuild(aDefaults)
Set oCustom = DictBuild(aCustom)
For Each vKey in oDefaults
If oCustom.Exists(vKey) Then
oDefaults.Item(vKey) = oCustom.Item(vKey)
End If
Next
Set GetOpts = oDefaults
End Function
View as plain text
Visual Basic:
Public Function DictBuild(ByRef aArray)
bWord = True
Dim oDict
Set oDict = CreateObject("Scripting.Dictionary")
Dim sWord, sDef
If isArray(aArray) Then
For Each sString in aArray
If bWord Then
sWord = sString
bWord = False
Else
sDef = sString
oDict.Add sWord, sDef
bWord = True
sWord = NULL
sDef = NULL
End If
Next
End If
set DictBuild = oDict
End Function
