Dictionary Object stores data key, item pairs. Each item is associated with
a unique key.The key is used to retrieve an individual item and is usually a
integer or a string, but can be anything except an array.
Dictionary Object stores data key, item pairs. Each item is associated with a unique key.The key is used to retrieve an individual item and is usually a integer or a string, but can be anything except an array.
Creating a dictionary object:
We can create an instance of dictionary object as shown below:Set odict = CreateObject("Scripting.Dictionary")
odict.add "Capital", "Delhi"
We have created a dictionary object odict, and added 2 key item collection in the dictionary object.
Below are the methods of dictionary object:
1. Add Method - Adds a key and item pair to a Dictionary object.
odict.add "Currency", "Rupee"2. Exists Method - Returns true if a specified key exists in the Dictionary object, false if it does not.
odict.exists(“country”) will return true for dictionary defined in above examples.
3. Items Method - Returns an array containing all the items in a Dictionary object.aite = odict.items(“country”)
4. Keys Method - Returns an array containing all existing keys in a Dictionary object.aite = odict.keys(1)
5. Remove Method - Removes a key, item pair from a Dictionary object.odict.remove("Currency")
6. RemoveAll Method - The RemoveAll method removes all key, item pairs from a Dictionary object.odict.removeAll()
Properties of Dictionary object
1. Count Property - Returns the number of items in a collection or Dictionary object.Intcount = odict.count
2. Item
Property - Sets or returns an item for a specified key in a Dictionary
object. For collections, returns an item based on the specified key.ItemVal = odict.Item(
"Capital")
- gets
value of the keyodict.Item(
"Capital")
= “MUMBAI” – sets value of the key.
3. Key Property - Sets a key in a Dictionary
object.odict.Key(
"Capital")
= "City"odict.item(“City”)
will now give value of item stored in key Capital before.
Comparing Dictionaries Vs Arrays
- Dictionary has methods to add new items and check for existing items.
- On Deleting a particular item from a dictionary, all the subsequent items automatically shift up.
- You use keys to identify dictionary items. Keys can be any data type while arrays have numeric values as key
- A dictionary can't be multidimensional while array can be.
- It is much easier to find an item value based on keys as compared to arrays.
- Keys in Dictionary can have unique values only
- Dictionary can be used as Global variable declaration. so that any test can access the values from it in the run time. Will discuss this in detail in a separate post.