Optional arguments in VBScript - QTP

VBScript does not support optional parameters but there are some workarounds that can be used to achieve similar functionality:

  • Method 1 - Optional arguments using Null/Empty values
  • Method 2 - Optional arguments using Array members
  • Method 3 - Optional arguments using an Array of (key, value) pairs
  • Method 4 - Optional argument using an Array of (key:=value) pairs
  • Method 5 - Optional arguments using an Array of Hybrid (key. value or key:=value) pairs

Using Null/Empty values

This method uses Null or Empty value for optional parameters.

'This method checks if its parameter is a Null or a Empty value.
Public Function IsMissing(ByValParam)
On error resume next
'Check if the parameter is null or empty
IsMissing = IsEmpty(Param) or IsNull(Param)
'In case of error assume that it's a objectand return false If err.Number<> 0 Then
IsMissing = False
End Function
'Demonstrate Method 1 - Optional arguments using Null/Empty values Public Function OptParameter_Methodl (ByValParaml, ByVal Param2)
If IsMissing(Paraml) Then Paraml = "Tarun"
If IsMissing(Param2) Then Par^m2 = "Laliani"
MsgBoxParaml& " " 6 Param2 End Function
'Usage:
Call OptParameter_Methodl("Tarun", Null) Call OptParameter_Methodl("Tarun", "Lalwani"") Call OptParameter_Methodl(Empty, "Lalwani")

Using an Array of values

This method defines an array as the input parameter and then uses one or more Array elements and the actual parameters:

'Method 2 for optional arguments
Public Function 0ptParameter_Method2 (Arguments)
'Take the values from arguments
FirstName = Arguments(a)
LastName = Arguments(l)
If IsMissing(FirstName) Then FirstName = "Tarun" If IsMissing(LastName) Then LastName = "Lalwani"
'Do processing on the data.
'Incase you want to implement the ByRef then Reassign
'the values back to the array
Arguments(0) = FirstName
Arguments(l) = LastName
MsgboxFirstName& " " &LastName End Function
'Usage:
Call 0ptParameter_Method2 (Array("Tarun",Null)) Call 0ptParameter_Method2 (Array(Null,Null))

The problem with this approach is that if the last parameter is missing we have to pass a Null value every time. We can improve that by expanding the array automatically in case there are missing parameters:

'Method 2A for optional arguments Public Function0ptParameter_Method2A (Arguments) ConstParamCount = 2
Dim Params
'Check if no array has been passed If IsMissing(Arguments) Then
ReDimPararas(ParamCount - l) Else 'In case Arguments is nc^t an Array, then make it an Array If VarType(Arguments) <vbArray Then Arguments = Array(Arguments)
End If
Params = Arguments
'Resize the arguments array to get the expected parameter length 'Use preserve to prevent loss of other arguments ReDim Preserve Params(ParamCount - 1)
End If
'Not repeating the code here Call 0ptParameter_Method2(Params)
End Function
'Usage:
Call 0ptParameter_Method2A (Null)
Call 0ptParameter_Method2A ("Tarun")
Call 0ptParameter_Method2A (Array(Null, "Lalwani"))
Call 0ptParameter_Method2A (Array("Tarun"))

•• Using an Array of (Key, Value) pairs

In this method we pass the arguments as a value pair, one as the name and another as its value. The number of arguments in this case needs to be even. We would create a dictionary from the array and use it as parameters. Let's first look at the parameter conversion from array to dictionary.

wait 'Method 2A for optional arguments Public Function 0ptParameter_Method2A (Arguments) ConstParamCount = 2 Dim Params 'Check if no array has been passed If IsMissing(Arguments) Then
ReDimParams(ParamCount - 1) Else 'In case Arguments is not an Array, then make it an Array If VarType(Arguments) <vbArray Then Arguments = Array(Arguments)
End If
Params = Arguments
'Resize the arguments array to get the expected parameter length
'Use preserve to prevent loss of other arguments ReDim Preserve Params(ParamCount - 1)
End If
'Not repeating the code here Call 0ptParameter_Method2(Params)
End Function
'Usage;
Call 0ptParameter_Method2A (Null)
Call 0ptParameter_Method2A ("Tarun")
Call 0ptParameter_Method2A (Array(Null, "Laiwani"))
Call 0ptParameter_Method2A (Array("Tarun"))
Now let's look at how to use the above techniques
'Method to demonstrate
Public Function 0ptParameter_Method3 (Arguments) Set Params = CreateObject("Scripting,Dictionary")
GetArgumentsList_vlParams, Arguments
FirstName = GetParamValue(Params, "FirstNasste", "Tarun") LastName = GetParamValue(Params, "LastName", "Lalwani")
MsgBoxFirstName& " " &LastName
End Function ,
'Usage:
Call 0ptParameter_Method3 (Null)
Call 0ptPaxameter_Method3 (Array ("Firstfeisie" , "Tarun" , "LastMaiie" , "Lalnsani"))
Call 0ptParameter_Method3 (Array("Pirstfesie", "Tarun"))
Call 0ptParameter_Method3 (Array("LastName","Lalwani"))

Using an Array of Key:=VaIue pairs

In this method iunction arguments are passed as key:=value pairs. The function which converts the massed values to a dictionary would change:

'This Function creates a dictionary from the array Public Sub GetArgumentsList_v2(ArgListDict, Arguments)
'Set the compare mode as text so that parameters
'are in-case sensitive
ArgListDict.CompareMode = vbTextCompare
'If argument is missing then do not process If IsMissing(Arguments) Then Exit Sub
Dim i, iCount
iCount = UBound(Arguments)
For i = LBound(Arguments) to iCount step 1
valPos = InStr(Arguments(i), ":•")
ParamName = Left (Arguments(i), valPos - l)
ParamValue = Mid (Arguments(i), valPos + 2)
ArgListDict.AddParamName, ParamValueNext End Sub
'Method to demonstrate
Public Function 0ptParameter_liethod4 (Arguments) Set Params = CreateObjectC"Scripting,Dictionary")
GetArgumentsList_v2 Params, Arguments
FirstName = GetParamValueCParams, "FirstName","Tarun") LastName = GetParamValueCParams, "LastNaite", "Lalwani")
MsgBoxFirstName& " " &LastNameEnd Function

'Usage:

Call 0ptParameter_Method4 (Null)
Call 0ptParameter_Method4 (Array ("FirstNaise:=Taron","LastNaaes=Lalwani"))
Call 0ptParameter_Method4 (Array("FirstNaaes=Tarun"))
Call 0ptParameter_Method4 (Array("LastNaisei =Lalwani"))

Arrays of Hybrid (Key, Value or Key:=Value) pairs

In this method we support passing flinction parameters using both (Key,Value) or Key:=Value pairs. The advantage of this method is the ability to pass arguments in any order and the ability to pass objects at the same time using (Key, Value) pairs.
'This Function creates a dictionary from the array
Public Sub GetArgumentsList_v3(ArgListDict, Arguments)
'Set the compare mode as text so that parameters
'are in-case sensitive
ArgListDict.CompareMode = vbTextCompare
'If argument is missing then do not process If IsMissing(Arguments) Then Exit Sub
Dim i, iCountiCount = UBound(Arguments) For i = LBound(Arguments) to iCount step 1 valPos = InStr(Arguments(i), "is")
If valPos = 0 Then
'It's a key,value pair
ParamName = Arguments(i)
'Move to the value
i = i + 1 ParamValue = Arguments(i) Else
'It's a Key:=Value pair
ParamName = Left (Arguments(i), valPos - 1)
ParamValue = Mid (Arguments(i), valPos + 2) End if
ArgListDict.AddParamName, ParamValue Next End Sub
'Method to demonstrate
Public Function OptParameter_Method5 (Arguments)
Set Params = CreateObject("Scripting. Dictionary")
GetArgumentsList_v3 Params, Arguments
FirstName = GetParamValue(Params, "FirstNaae","Tarun") LastName = GetParamValue(Params, "LastName", "Lalwani"1)
MsgBoxFirstName& " " &LastNameEnd Function
'Usage:
Call 0ptParameter_Method5 (Null)
Call OptParameter_Method5 (Array("FirstName:=Tarun","LastName:=Lalwani"))
Call OptParameter_Method5 (Array("Firstname: sTarun"))
Call 0ptParameter_Method5 (Array("LastNaies=Lal«ani"))


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

QTP Topics