TESTEVERYTHING

Saturday, 21 May 2011

Dictionary Object in QTP

Dictionary object is similar to a typical array. The difference between a dictionary object and an array is that there is a unique key associated with every item of dictionary object. This unique key can help you in calling that item as and whenever required.

In arrays we store the values based upon the index i.e. 0,1,2 but here we can store the values based upon the names i.e. keys. Each value is stored corresponding to the unique keys. Dictionary object that stores data in key, item pairs.

Dictionary objects are very useful when dealing with the large no of data. On an average they are 3 times faster then the collection objects

Every key should be unique. Duplicate keys are not allowed. Dictionary object belong to "Scripting.Dictionary" class. To create a dictionary object we can use the following statement:

REM Create a variable.
Dim objDict
REM Create dictionary object
Set objDict=CreateObject("Scripting.Dictionary")

Following are the methods available with the Dictionary Objects:
Add Method,Exists Method, Items Method, Keys Method, Remove Method, RemoveAll Method

REM  Adding keys and corresponding items. 

objDict.Add “Company”, “TEST”
objDict.Add “Name”, “Rajiv”
objDict.Add “Blog”, “QTP” 




Using Exists Method to check whether the key 'Company' exists?

If  objDict.Exists(“Company”) Then
   msgbox “Specified key exists.”
Else
   msgbox “Specified key doesn’t exist.”

End If"

Retrieve ALL items and keys respectively from inside dictionary object.

REM Get the items.
allItems = objDict.Items
REM  Get the keys.
allkeys= objDict.Keys

REM Iterate the array.
For x = 0 To objDict.Count-1
    msgbox  allItems(x) & ” :” & allkeys(x)
Next

Using Remove method

objDict.Remove("Company")

 Now it will return false because we have deleted the key.

  msgbox objDict.Exists(“Company”)

Using Remove all method to clear the dictionary

REM Clear the dictionary.
objDict.RemoveAll

1 comment:

Unknown said...

Please clarify my doubt,
I Have 2 dictionary objects, 1st oDict1 contains 60 keys and their respective values but oDict2 contains only 10 key values ...
Now i would like to compare oDict2 keys to oDict1 keys then i would like to get values of those keys of odict2 from odict1... ?
Thanks in advance... here i got solution but mainly i would like to which way i can retrieve faster

Post a Comment

Which one is right ?

Translate







Tweet