arrKeys = objDictionary.Keys
arrValues = objDictionary.Items
The last couple of days I have tried to learn a bit about Classes in VBScript and I noticed that a class public variables are available in QTP's Debug Variables tab in alphabetic order. That made me come up with this crazy idea to maybe replace the use of Dictionary object with a custom class instead...but Classes in VBScript seems a bit limited according to my requirements. However, for debugging purposes it could be nice to create a "Class copy object" of a Dictionary:
'Create a dictionary object and add some parametersAbove code example is available for download here: http://abouttesting.fileave.com/DictionaryClass.vbs
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "A_Parameter","47"
objDictionary.Add "Z_Parameter",""
objDictionary.Add "C_Parameter","11"
'Create a class out of the dictionary and a new debug object
Execute CreateClass("DebugDictionary", objDictionary)
Set objDictionaryDebug = New DebugDictionary
'Display A_Parameter value
Msgbox objDictionaryDebug.C_Parameter
Set objDictionaryDebug = Nothing
Set objDictionary = Nothing
Function CreateClass (strClassName, objDictionary)
'* Create Class string from Dictionary object
'* By Stefan Thelenius 2008-04-02
'* Usage example: Execute CreateClass("MyClass", objDictionary)
'Get keys and values into arrays
arrKeys = objDictionary.Keys
arrValues = objDictionary.Items
For i = 0 to UBound(arrKeys)
'Create Public variables
strClassPublicVar = strClassPublicVar & "Public " & arrKeys(i) & vbLf
'Set default values to variables
strClassPublicInit = strClassPublicInit & arrKeys(i) & "=""" & arrValues(i) & """" & vbLf
Next
'Add number of Keys as Count Int
strClassPublicVar = strClassPublicVar & "Public Count" & vbLf
strClassPublicInit = strClassPublicInit & "Count=" & UBound(arrKeys)+1 & vbLf
'Set Class and initializer
strClassStart = "Class " & strClassName & vbLf
strClassEnd = "End Class"
strClassInitStart = "Private Sub Class_Initialize" & vbLf
strClassInitEnd = "End Sub" & vbLf
'Return class string
CreateClass = strClassStart & strClassPublicVar & strClassInitStart & strClassPublicInit & _
strClassInitEnd & strClassEnd
End Function
When using QTP the debug view looks like this:
1 comment:
Pretty helpful material, much thanks for this article.
Post a Comment