All About Dictionary Object in QTP/UFT

There are many ways to use Dictionary Objects but the most common use of Dictionary Object is to keep the value in an Array and refer each value with a unique Key or Index. Basically, Dictionary Object is nothing but an Array which contains values which are referred by unique keys. For each value in Dictionary Object, there is one unique key associated with value. Using Key name, we can call associated value anywhere in the script.

This is the main difference between Array and Dictionary Object. An Array can have index value as numeric whereas in dictionary object, index value can be string. Hence while referring any value with key, a key can have any name associated with a value.

 Array

DicObj1

Dictionary Object

DicObj2

Once you create Dictionary Object, there are many Dictionary Object Methods can be applied as per requirement in the script. Refer the screen shot given below for Methods of Dictionary Object and later we will discuss about all the Dictionary Object Methods in details.

How To Create Dictionary Object:

Creating Dictionary Object is very simple. Type the below code in your script to create a Dictionary Object:

Dim DicObj
Set DicObj=CreateObject("Scripting.Dictionary")

Dictionary Object Methods:

DicObj3

Add : Used to add new item in Dictionary Object.

Example:

DicObj.Add "Name", "Testingfreak"

DicObj4

Exists: Verifies whether the key or value exists or not in the array.

Example:

If DicObj.Exists("Name") then
MsgBox "Key Exists"
End If

DicObj5

Items: Returns an Array with all the values in Dictionary.

Example:

DicObj.Add "Name", "Testingfreak"
DicObj.Add "Country", "Sweden"
I = DicObj.Items
MsgBox I(1)

DicObj6

Keys: Returns an Array with all the available Keys in Dictionary.

Example:

DicObj.Add "Name", "Testingfreak"
DicObj.Add "Country", "Sweden"
K = DicObj.Keys
MsgBox K(1)

DicObj7

Remove: Removes an item associated with specified key in Dictionary.

Example:

DicObj.Add "Name", "Testingfreak"
DicObj.Add "Country", "Sweden"
DicObj.Remove("Country")
MsgBox DicObj.Item("Country")

DicObj8

RemoveAll: Removes item associated with all the Key from Dictionary.

Example:

DicObj.Add "Name", "Testingfreak"
DicObj.Add "Country", "Sweden"
DicObj.RemoveAll
MsgBox DicObj.Item("Country")

DicObj9

Here all the items have been removed, hence in message box values is showing blank.

Leave a Reply

Your email address will not be published. Required fields are marked *