Implementing optional and default parameters (QTP, VBScript)

Categories: How to

Original date: 16 Dec 2008, 1:14pm

Service function

Public Function AssociateParameters(ByVal strParams)
 Dim objDictionary, Values, strPair, Pair
 Dim intCount, i
 strParams = Replace(strParams, "\,", Chr(176))
 strParams = Replace(strParams, "\=", Chr(187))
 Values = Split(strParams, ",")
 intCount = UBound(Values)
 Set objDictionary = CreateObject("Scripting.Dictionary")
 For i=0 To intCount
 strPair = Trim(Values(i))
 Pair = Split(strPair, "=")
 If UBound(Pair) = 1 Then
 If Not objDictionary.Exists(Trim(Pair(0))) Then
 objDictionary.Add Trim(Pair(0)), Replace(Replace(Trim(Pair(1)), Chr(176), ","), Chr(187), "=")
 Else 
 objDictionary.Item(Trim(Pair(0))) = Replace(Replace(Trim(Pair(1)), Chr(176), ","), Chr(187), "=")
 End If
 End If
 Next
 Set AssociateParameters = objDictionary
End Function

Implementation

Public Function SetWindow(ByRef objWin, ByVal objParameter)
Dim boolRC
Dim sTypeName
Dim intSync, boolSync
Dim sWinState, intX, intY
'Verify parameters
sTypeName = TypeName (objParameter)
If sTypeName <> "Dictionary" Then
Set objParameter = CreateObject("Scripting.Dictionary")
End If
'--First optional parameter--
'Sync
If objParameter.Exists("p.sync") Then
 boolSync = TRUE
 intSync = CInt(objParameter.Item("p.sync"))
 If intSync <= 0 Then
 intSync = 300
 End If
Else
 boolSync = FALSE
End If
If boolSync Then
 objWin.WaitProperty "enabled", 1, intSync
End If
boolRC = objWin.Exist(0)
If Not boolRC Then
 SetWindow = FALSE
 Exit Function
End If
'Retrieve arguments
sWinState = UCase(objParameter.Item("state"))
'--HERE WE'RE DEFAULTING--
If sWinState = "" Then
 sWinState = "FOCUS"
End If
Select Case UCase(sWinState)
 Case "FOCUS"
 objWin.Activate
 Case "MIN"
 objWin.Minimize
 Case "MAX"
 objWin.Maximize
 objWin.Activate
 Case "RESTORE"
 objWin.Restore
 objWin.Activate
 Case "RESIZE"
 '--More optional parameters--
 intX = CInt(objParameter.Item("p.width"))
 intY = CInt(objParameter.Item("p.height"))
 objWin.Resize intX, intY
 objWin.Activate
 Case "MOVE"
 '--More optional parameters--
 intX = CInt(objParameter.Item("p.x"))
 intY = CInt(objParameter.Item("p.y"))
 objWin.Move intX, intY
 objWin.Activate
 Case Else
 objWin.Activate
End Select
SetWindow = TRUE
End Function

Examples

'Just verify the window exists and set focus on
boolRC = SetWindow(Window("Test Window"), Nothing)
'Verify the window exists and maximize
boolRC = SetWindow(Window("Test Window"), AssociateParameters("state = MAX"))
'Verify the window exists and move
boolRC = SetWindow(Window("Test Window"), AssociateParameters("state = MOVE, p.x = 10, p.y = 10"))

PS. Later on I published an article describing this approach and how to use function overloading in VBScript.


  • Leave a Reply

    * Required
    ** Your Email is never shared

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by the author is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.