Thursday, January 3, 2019


Dictionary Object in vb script Example

Dictionary object is used to store the data in key-item pairs in qtp.

A Dictionary object is just like an associative array.
It stores the data in the form of key-item pairs.
Each key has some data item associated with it.
Key can be integer or string format.
Data item can be integer or string or array of variants. It may contain other dictionary itself.

Methods of Dictionary Object

Add ---------> Adds new key-item pair in the dictionary object
Exists --------> returns true if the given key exists in the dictionary object
Items --------> returns the array containing all items in the dictionary object
Keys --------> returns the array containing all keys in the dictionary object
Remove -----> removes the key-item pair with given key from the dictionary object
RemoveAll --> removes all key-item pairs from the dictionary object

Properties of Dictionary Object

Count-----------> returns the total number of keys in the dictionary object
Item-------------> assigns or returns the item value with given key from the dictionary object
Key-------------> sets the new key value for the given key
CompareMode -> assigns or returns the comparison mode for comparing string keys in a Dictionary object.

Example with dictionary object in VBScript

'create new dictionary object
Set dictionaryObject = CreateObject("Scripting.Dictionary")

dictionaryObject.CompareMode = 1

'add some key-item pairs
dictionaryObject.Add "1", "Sagar"
dictionaryObject.Add "2", "Amol"
dictionaryObject.Add "3", "Ganesh"

dictionaryObject.Key("3")  = "Bro"

Msgbox dictionaryObject.Item("Bro")

Msgbox dictionaryObject.Item("bro")

If  dictionaryObject.Exists("1") Then
 Msgbox "Dictionary contains key 1"
Else
 Msgbox "Dictionary does not contain key 1"
End If

'Display keys and items  in the dictionary
for each k in dictionaryObject.keys

 Msgbox  k & " - " & dictionaryObject.item(k)

next


'Display items
for each i in dictionaryObject.items

 Msgbox  i

next


Msgbox "Total number of keys in the dictionary are -> " & dictionaryObject.Count


'Remove the key-item pair with key = 1  from the dictionary object
dictionaryObject.Remove("1")

'Remove all key-item pairs from the dictionary object
dictionaryObject.RemoveAll

'Release the object
Set dictionaryObject = nothing

Please give your inputs, suggestions, feedback to Us about above VBScript topic. We value your thoughts.



0 comments:

Post a Comment

Blog Archive

Translate

Popular Posts

Total Pageviews

Blog Archive