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 “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