Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Basics of dictionary object
1. Dictionary Object In QTP :
The Dictionary object is used to hold a set of data values in the form of (key, item) pairs.
It is also known as 'Associative Array' because it associates a key with an item.
If you compare it with an Array, Keys are similar to the indices in an array. But here Keys are not
numetic Indices like we have in array ranther than these are
arbitrary strings normally. Each key in a single Dictionary object must be unique.
Dictionary Object is a part of VB Script and so it can be used with the tools supporting VBS.
(Here we will talk how it used in QTP).
Dictionary object is COM object(Component Object Model) and “Scripting.Dictionary” is the ProdID
for Disctionary object.
It can be instantiated as following:
Dim oDict
Set oDict = CreateObject("Scripting.Dictionary")
Using Add method we can add fdifferent keya and pair values to the structure.
Set oDict = CreateObject("Scripting.Dictionary")
oDict.Add "Tool1", "Selenium"
oDict.Add "Tool2", "QTP"
oDict.Add "Tool3", "Silk Test"
Then it should look like
Key Value
Tool1 Selenium
Tool2 QTP
Tool3 Silk Test
To fetch the data from the Dict object we can use .Item method.
Msgbox oDict.item("Tool2") -- Should return respective item value i.e QTP
The problem with Dictionary object is, if you try to access any item which is not even
present in the Dictionary,it does not result in an error. Instead, the non-existent element
is added to the Dictionaty object.
Set oDict = CreateObject("Scripting.Dictionary")
oDict.Add "Tool1", "Selenium"
oDict.Add "Tool2", "QTP"
oDict.Add "Tool3", "Silk Test"
MsgBox oDict.Item("Tool4")
2. Here no ArrayIndexOutOfBoundsException occurs, rather than Tool4, is added to the Dictionary, along
with the item value Null.
Looping through the Items in Dictionary Object:
Dim oItem
Dim sItem, sMsg
Dim oDict
Set oDict = CreateObject("Scripting.Dictionary")
Set oDict = CreateObject("Scripting.Dictionary")
oDict.Add "Tool1", "Selenium"
oDict.Add "Tool2", "QTP"
oDict.Add "Tool3", "Silk Test"
For Each oItem In oDict
sItem = oDict.Item(oItem )
sMsg = sMsg & sItem & vbCrLf
Next
MsgBox sMsg
Dictionry object having only one configurable property, Compare Mode.
If its is created in binary mode, which means each key in the Dictionary is based on its ASCII value.
Means lowercase and upper cases are treated as different here.
When a Dictionary is configured in text mode, uppercase and lowercase letters are treated identically.
0 – Sets the mode to binary. This is the default value.
1 – Sets the mode to text.
Set oDict = CreateObject("Scripting.Dictionary")
oDict.CompareMode = 1
Initially while creating only youi can set the compare mode.Once it is alreday having any element, you
are not allowed
to change the compare mode.
To reconfigure the Dictionary mode, you first need to remove all items from the Dictionary.
To remove a value from a dictionary, use the .Remove method and specify the key to remove. For
example:
oDict.Remove “Tool2"
To remove all values and clear the dictionary, use the .RemoveAll method. Use the .Count property to
obtain a count of values in the dictionary.
3. The .Keys and .Items methods return an array containing all the keys or items from the dictionary. For
example:
aTools = oDict.Keys
aNames = oDict.Items