Dictionary Object
Arrays are the first construct that VBScript instructors introduce when they discuss how to group data. With arrays, you can store columns of data in one place, then access the data later through one variable. However, years of real-world use have revealed that arrays aren't always the most desirable solution to gather and maintain related data. Fortunately, a new type of array has emerged: the dictionary. Here's a look at what dictionaries are and how you manipulate them with the methods and properties.
| The Dictionary Object's Methods and Properties | |
| Method or Property | Description |
| Methods | |
| Add | Adds a new item to the dictionary |
| Exists | Verifies whether a given key exists in the dictionary |
| Items | Returns an array with all the values in a dictionary |
| Keys | Returns an array with all the keys in a dictionary |
| Remove | Removes the item identified by the specified key |
| RemoveAll | Removes all the items in the dictionary |
| Properties | |
| Count | Returns the number of items in a dictionary |
| Item | Sets and returns an item for the specified key |
| Key | Changes an item's key |
'***************************************************************'Creation of a Dictionary ObjectDim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
Here 'd' is a variable which is converted into a dictionary object.'*************************************************************** '***************************************************************'Adding items to Dictionary ObjectSyntax:
Object.Add Item,ValueDim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
'*************************************************************** '***************************************************************'To check for Key Exist or notDim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
If d.Exists("c") Then
msgbox "key exists"Elsemsgbox "key doesn't exist"End If
'***************************************************************'***************************************************************'To get values of Items Dim a, d, i, iListSet d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
iList = d.Items
For i = 0 To d.Count -1
msgbox iList(i)
Next'***************************************************************'***************************************************************'To get names of the Keys Dim a, d, i, iListSet d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
iList = d.Keys
For i = 0 To d.Count -1
msgbox iList(i)
Next'***************************************************************'*************************************************************** 'To Remove a key from Dictionary Object Dim a, dSet d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
d.Remove("b") ' Remove second pair.
'*************************************************************** '***************************************************************'To Remove all keys from Dictionary Dim dSet d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
d.RemoveAll ' Clear the dictionary.'*************************************************************** '***************************************************************'To get value of Single Key Dim dSet d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
msgbox d("a") or msgbox d.Item("a")
'*************************************************************** '***************************************************************'Using Dictionary object in functions Set UDetails=CreateObject("Scripting.Dictionary")
UDetails.add "UserName","Sudhakar"
UDetails.add "Password","qtp"
'Here is a dictionary object with user name and password. 'To use these details in a function we should develop functions in this format.Function Login(UserDetails)Browser(bName).Page(pName).webedit(uName).set UserDetails("UserName")
Browser(bName).Page(pName).webedit(pwd).set UserDetails("Password")
Browser(bName).Page(pName).webbutton(bName).click
End Function
Calling the created functioncall Login (Udetails)'*************************************************************** '***************************************************************
Good job dude!
ReplyDeleteThis is awesome.
ReplyDeletereally good one, very useful.
ReplyDeleteThanks a lot.
Where we store the defined Dictionary objects, is it in function library or somewhere else?
Vishnu
Excellent
ReplyDeleteNice Article.Very Useful.
ReplyDeleteThanks for sharing!!!!!!
its good for every one...thanks a lot
ReplyDeletevery useful article.
ReplyDeletehow can we use Dictionary objects in multiple actions
ReplyDeleteTHANK YOU !!!!
ReplyDeleteHi,
ReplyDeleteI am new to QTP. Trying to automate WebTable which scenarios.
I have to add, modify and delete rows in WT. Every time I make a modification. I will re construct the object to get correct row count.
My problem is after certain iternations. WT object is not recognised. I am on QTP11/IE8. Any insight will be greatly apprecited.
Thanx,
Thanks a lot. So nicely explained
ReplyDelete