Reputation: 7625
Is there ways to create optional arguments to functions in vb script allowing you to write functions something like...
myFunc("happy")
myFunc("happy", 1, 2, 3)
myFunc("happy", 1)
etc.
Upvotes: 31
Views: 44805
Reputation: 1
To utilize optional parameters in a VBScript function you can use the IsArray function to determine if the passed argument is a single value or an array of arguments.
The below is an example of a function that has two required arguments, namely argRequired and argFields (assuming the latter is not an array). Any number of required arguments can be added to the function through explicit specification.
Function myFunc(ByVal argRequired, ByVal argFields)
Field_0 = argRequired
Field_1 = "Default 1"
Field_2 = "Default 2"
Field_3 = "Default 3"
Field_4 = "Default 4"
' ...
If IsArray(argFields) Then
For I = 0 to UBound(argFields)
Select Case I
Case 0 Field_1 = argFields(I)
Case 1 Field_2 = argFields(I)
Case 2 Field_3 = argFields(I)
Case 3 Field_4 = argFields(I)
' ...
End Select
Next
Else
Field_1 = argFields
End If
' my code
End Function
To call the function without optional parameters:
myFunc("first", 100)
To call the function with optional parameters, encapsulate the last required argument and all optional arguments in an array:
myFunc("first", Array(100, "third", 200, False))
In the first case, myFunc will see that the second argument is not an array and process it as an item.
In the second, myFunc processes the second argument as an array of arguments and parses the array items accordingly. The Select statement is used to allow a variable number of optional arguments in the array, vs. a requiring a fixed array size.
Note: If only one argument is required, argRequired may be omitted:
Function myFunc(ByVal argFields)
Field_1 = "Default 1"
Field_2 = "Default 2"
' ...
If IsArray(argFields) Then
For I = 0 to UBound(argFields)
Select Case I
Case 0 Field_0 = argFields(I)
Case 1 Field_1 = argFields(I)
Case 2 Field_2 = argFields(I)
' ...
End Select
Next
Else
Field_0 = argFields
End If
' my code
End Function
Upvotes: 0
Reputation: 12395
The optional keyword (like in VB6) is not allowed in vbscript
maybe this helps: https://web.archive.org/web/20210304114036/http://www.4guysfromrolla.com/webtech/071801-1.shtml
Upvotes: 19
Reputation: 13213
You can always make it a class
and use Public Property Let
to populate your Sub
/Function
before calling it:
Set oSubName = New cSubName
'fill your parameters, you can always add more later
oClass.OptionalParameter1 = true
oClass.OptionalParameter2 = false
'execute sub
oSubName.Execute
Set oSubName = Nothing
This would require some knowledge on how to make classes, but is probably the next best solution to using arrays.
Good luck.
Upvotes: 3
Reputation: 1
Just pass a different type and look it.
wscript.echo "1: " & math(7,8,false)
wscript.echo "2: " & math(7,8,5)
wscript.echo "3: " & math(15,false,5)
function math( Addend1 , Addend2, Divisor )
dim tmpTotal
if Addend2 then
TmpTotal = Addend1 + Addend2
else
tmpTotal = Addend1
end if
if Divisor then 'if argument Divisor is other than False then do it.
if Divisor > 0 then 'Hated Divide by Zero since VIC-20.
tmpTotal = tmpTotal / Divisor)
end if
end if
end function
Upvotes: -5
Reputation:
Obviosly this depends on your environment and whether it supports using both JScript and VBScript in the same file, but I've had some success using Windows Script Host (*.wsf files), ie
<?xml version="1.0" standalone="yes" ?>
<package xmlns="Windows Scripting Host">
<job id="param">
<?job debug="true"?>
<script language="JavaScript">
<![CDATA[
function MakeString(args) {
var s = [];
for(var i = 0, length = arguments.length; i < length; i++)
s.push(arguments[i]);
return s.join('');
}
]]>
</script>
<script language="vbscript">
<![CDATA[
WScript.Echo MakeString("hello", " ", "world")
WScript.Echo MakeString()
WScript.Echo MakeString(1,2,3,4)
]]>
</script>
</job>
</package>
where you can define your function in JScript and reference it in VBScript. A better way may well be to include your JScript functions as an external file ie
<script language="JavaScript" src="makestring.js"/>
<script language="vbscript">
<![CDATA[
WScript.Echo MakeString("hello", " ", "world")
WScript.Echo MakeString()
WScript.Echo MakeString(1,2,3,4)
]]>
</script>
Upvotes: 1