Saturday, January 5, 2013

How to work with Dictionary object using QTP

In this video, i demonstrated about how to work with Dictionary object:


Here is the code i used for demonstration:

Set dictObj=Createobject("Scripting.Dictionary")

dictObj.Add "Name","Uday"
dictObj.Add "Age",32
dictObj.Add "Loc","India"
dictObj.Add "Height",5.10
dictObj.Add 1,"10" 'Observe here i am passing key as a integer

print dictObj.Count 'Count is dictionary property which retrieves number of  items in the collection

print dictObj.Item("Name") 'Item property retrieves or sets the value of the key.
dictObj.Item("Name")="Udaya Kumar Anem"
print dictObj.Item("Name")

dictObj.Key("Loc")="Location" 'We can use key property to update the key value
print dictObj.Item("Location")

aKeys=dictObj.Keys  'This method returns all the keys in the collection as an array
For i=0 to ubound(aKeys)
    print aKeys(i)
Next

For i=1 to dictObj.Count
    print dictObj.Item(aKeys(i-1)) 'This method retrieves the value of the key
Next

aValues=dictObj.Items  'This method returns all the Values in the collection as an array
For i=0 to ubound(aValues)
    print aValues(i)
Next

dictObj.Remove(1) 'This method removes the key value pair from the collection

If dictObj.Exists(1)  Then 'This method looks for the existance for a Key
    print "Key exists"
else
    print "Key does not exist"
End If

dictObj.RemoveAll 'This method removes all key value pairs from the collection i.e clearing the dictionary

No comments:

Post a Comment