Published: Wednesday, July 18, 2001
Optional Arguments in VBScript
By Francis Chau
Introduction:
A frequently asked question about VBScript is "How can I make an argument of a Function or Sub
optional?" and matter of factly, it's been answered on ASPFAQs.com at
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=42.
The answer to this question? VBScript does not support optional arguments, however, JScript does
and the FAQ shows an example of this in JScript.
To circumvent this VBScript limitation, I will show you a technique that can be used to simulate
optional arguments in VBScript that I call the Array-based approach. I'll also demonstrate the
more widely accepted NULL-checking approach.
Using the Array-Based Approach
To see how to use the array-based approach for creating subroutines with optional arguments,
let's start by an example subroutine:
Sub mySubRoutine(rArgs)
'Declare local variables
Dim v1,v2,v3,v4
'Initialize the local variables (assign
'them all to empty strings
v1 = "" : v2 = "" : v3 = "" : v4 = ""
Select Case UBound(rArgs)
Case 0
v1 = rArgs(0)
Case 1
v1 = rArgs(0)
v2 = rArgs(1)
Case 2
v1 = rArgs(0)
v2 = rArgs(1)
v3 = rArgs(2)
Case 3
v1 = rArgs(0)
v2 = rArgs(1)
v3 = rArgs(2)
v4 = rArgs(3)
End Select
Response.Write "v1 = " & v1 & "<br>"
Response.Write "v2 = " & v2 & "<br>"
Response.Write "v3 = " & v3 & "<br>"
Response.Write "v4 = " & v4 & "<br>"
End Sub
|
Notice in the subroutine declaration, we've only defined one argument, rArgs:
The argument rArgs will be an array of values we would like to pass into our
subroutine.
The next few lines declare our local variables and initializes them to default values (empty
strings, in this case).
Next, we use the UBound() function to determine the number of arguments passed.
Then assign the array elements to the local variables:
Select Case UBound(rArgs)
Case 0
v1 = rArgs(0)
Case 1
v1 = rArgs(0)
v2 = rArgs(1)
Case 2
v1 = rArgs(0)
v2 = rArgs(1)
v3 = rArgs(2)
Case 3
v1 = rArgs(0)
v2 = rArgs(1)
v3 = rArgs(2)
v4 = rArgs(3)
End Select
|
Since the array is zero-based, our first Case branch assigns the first element to
our v1 variable. As we need to add more arguments to our function, we can easily
add more Case branches.
We conclude our subroutine by printing out the values of our local variables.
To call this subroutine we need to create an array of the size based on the number of
arguments we wish to pass into the function, and then populate this array with the values we
wish to pass to the function. Finally, we pass along the created array as the single argument
to the function:
'Create the array to pass in to our sub
Dim myArray
'Call the subroutine with two arguments
Redim myArray(1)
myArray(0) = Value1
myArray(1) = Value2
mySubroutine myArray
|
Notice that you can also use the Array constructor syntax like so:
'Call the subroutine with three arguments
mySubroutine Array(Value1, Value2, Value3)
|
Now that we have a way to pass only the arguments we choose using the array-based method, let's
take a look at another approach at optional arguments: the NULL-checking approach, which we'll
examine in Part 2.
Read Part 2!