Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:optionalarguments [2015/09/14] (current)
Line 1: Line 1:
 +====== Optional Arguments to VBScript Subroutines and Functions ======
 +> **Developer:​** [[developer:​rhinoscript|RhinoScript]]
 +> **Summary:​** //​Demonstrates how to implement optional arguments in VBScript.//
 +
 +=====Question=====
 +How can I make an argument of a VBScript function or subroutine optional?
 +
 +=====Answer=====
 +In VBScript, the **Optional** keyword, which allows some arguments to be left out in Visual Basic, is not implemented. This means that you must declare every argument that you want to use.
 +
 +What you can do is pass null values to your functions. For example:
 +
 +<code vb>
 + ​Function SomeArgs(one,​ two, three, four)
 +   ​SomeArgs = one & two & three & four
 + End Function
 +
 + Call SomeArgs(1, 2, 3, Null)
 +</​code>​
 +
 +=====Discussion=====
 +To work around this limitation, you can use an array-based approach to simulate optional arguments. To see how to use the array-based approach for creating subroutines with optional arguments, consider the following example:
 +
 +<code vb>
 + Sub MySubroutine(arrArgs)
 +
 +   '​ Declare local variables
 +   Dim v1, v2, v3, v4
 +
 +   '​ Initialize the local variables with default values
 +   v1 = 1 : v2 = 2 : v3 = 3 : v4 = 4
 +
 +   ​Select Case UBound(arrArgs)
 +     Case 0
 +       v1 = arrArgs(0)  ​
 +     Case 1
 +       v1 = arrArgs(0)
 +       v2 = arrArgs(1)
 +     Case 2
 +       v1 = arrArgs(0)
 +       v2 = arrArgs(1)
 +       v3 = arrArgs(2)
 +     Case 3
 +       v1 = arrArgs(0)
 +       v2 = arrArgs(1)
 +       v3 = arrArgs(2)
 +       v4 = arrArgs(3)
 +     Case Else
 +       Exit Sub
 +   End Select
 +
 +   ​Rhino.Print "​v1 ​ = " & CStr(v1)
 +   ​Rhino.Print "​v2 ​ = " & CStr(v2)
 +   ​Rhino.Print "​v3 ​ = " & CStr(v3)
 +   ​Rhino.Print "​v4 ​ = " & CStr(v4)
 +
 + End Sub
 +</​code>​
 +
 +Notice in the subroutine declaration,​ only defined one argument has been defined:
 +
 +<code vb>
 + Sub MySubroutine(arrArgs)  ​
 +</​code>​
 +
 +The argument will be an array of values we would like to pass into the subroutine. The next few lines declare local variables and initializes them to default values (simple numbers, in this case). Next, use the UBound() function to determine the number of arguments passed. Then assign the array elements to the local variables:
 +
 +<code vb>
 +
 + ​Select Case UBound(arrArgs)
 +   Case 0
 +     v1 = arrArgs(0)
 +   Case 1
 +     v1 = arrArgs(0)
 +     v2 = arrArgs(1)
 +   Case 2
 +     v1 = arrArgs(0)
 +     v2 = arrArgs(1)
 +     v3 = arrArgs(2)
 +   Case 3
 +     v1 = arrArgs(0)
 +     v2 = arrArgs(1)
 +     v3 = arrArgs(2)
 +     v4 = arrArgs(3)
 +   Case Else
 +     Exit Sub
 + End Select
 +</​code>​
 +
 +Since the array is zero-based, the first case branch assigns the first element to our v1 variable. As more arguments to the function are needed, one can easily add more case branches.
 +
 +To call this subroutine, create an array of the size based on the number of arguments you want to pass into the function, and then populate this array with the values you want to pass to the function:
 +
 +
 +<code vb>
 + '​ Create the array to pass to MySubroutine ​
 + Dim arrArgs
 +
 + '​ Call the subroutine with two arguments
 + Redim arrArgs(1)
 + ​arrArgs(0) = Value1
 + ​arrArgs(1) = Value2
 +
 + Call MySubroutine(arrArgs)
 +</​code>​
 +
 +Or
 +
 +<code vb>
 + '​ Call MySubroutine with three arguments
 + Call MySubroutine(Array(Value1,​ Value2, Value3))
 +</​code>​
 +
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/optionalarguments.txt ยท Last modified: 2015/09/14 (external edit)