More on Named Optional Arguments in VBScript
Saturday, December 9th, 2006I have been extensively using the method for vbscript optional arguments that I described in March. I want to expand on this a little with some recent changes we have made to how we use this.
Functions that use this method have an aOptions parameter in their signatures. That means, that even if we are not going to specify any optional arguments, we have to pass something, so we pass a Null. The problem is that I end up passing Null most of the time, and I don't like having to do that - especially for our smoke tests where are code is visible to manual testers and developers.
For example, I will use a simple function called WebEditFillByID that simply finds a WebEdit based on it's html id and fills it with the text supplied. There is also an aOptions parameter in its signature, but I don't usually set any options. So, when I am filling out a form it looks something like this.
-
WebEditFillByID "firstName", "Bob", NULL
-
WebEditFillByID "lastName", "Saget", NULL
-
WebEditFillByID "ssn", "208-49-8168", NULL
That doesn't look too bad, but it would be much more legible without the Nulls in the way. So, to get aOptions out of WebEditFillByID's signature I create a new function called WebEditFillByIDAdv that is identical to WebEditFillByID. Directly beneath WebEditFillByIDAdv, I put a second function named WebEditFillByID with the same signature minus the aOptions. All WebEditFillByID does is call WebEditFillByID with the same arguments it was passed and adds a Null for the aOptions in WebEditFillByID's signature.
Now I can fill the same form without the Nulls.
-
WebEditFillByID "firstName" , "Bob"
-
WebEditFillByID "lastName" , "Saget"
-
WebEditFillByID "ssn" , "208-49-8168"
In my opinion, this is much more pleasant to deal with. For reference, here are the new functions.
-
Public Function WebEditFillByIDAdv ( sID, sValue, aOptions )
-
Set oOptions = GetOpts( _
-
ARRAY( _
-
"oBrowser", BrowserDefault(NULL) ), _
-
aOptions)
-
Set oBrowser = oOptions("oBrowser")
-
oBrowser.WebEdit("html id:=" & escapeHTMLid(sID)).Set sValue
-
End Function
-
-
Public Function WebEditFillByID ( sID, sValue )
-
WebEditFillByIDAdv sID, sValue, NULL
-
End Function
To read more about named optional arguments in vbscript, see my previous post about it.

