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.


Related Posts:

  • What are the different operators in vbscript? What are the different operators in vbscript? At broad level there are 3 kinds of operators as mentioned below. Arithmetic Operators Comparison Operators Logical Operators Arithmetic Operators in the order of pre… Read More
  • Data Types in vbscript Data Types in vbscript In vb script all variables are initialized with the data type called variant. But we can have below sub-types in vb script. In below table we have mentioned all sub data types in vb sc… Read More
  • Date time functions in vb script Date time functions in vb script It is very important that you know how to work with date and time in VBScript as most of the VBScript programs will have date and time involved in it.Below is the list of all date and … Read More
  • Classes in VBScript Classes in VBScript Here is an example that shows how we can create a class in QTP. Once we define the class, we can create its objects and then access its method and properties. 'declare the class book. Class Bo… Read More
  • Error handling in VBScript Error handling in VBScript VBScript provides below statements and keywords to handle errors. on error resume next Err object - err.description, err.number Sample code to handle the error is given below If er… Read More

0 comments:

Post a Comment

Blog Archive

Translate

Popular Posts

Total Pageviews

150,588

Blog Archive