Tuesday, 30 July 2013

Dictionary Object

Dictionary Object is a object that stores data key, item pairs.

A Dictionary object is the equivalent of a PERL associative array. Items can be any form of data, and are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.

Following methods can be used with Dictionary Objects:

1-Add- Adds a key and item pair to a Dictionary object.
2-Exists- Returns true if a specified key exists in the Dictionary object, false if does not.
3-Items- Returns an array containing all the items in a Dictionary object.
4-Keys- Returns an array containing all existing keys in a Dictionary object.
5-Remove- Removes a key, item pair from a Dictionary object.
6-RemoveAll- RemoveAll method removes all key, item pairs from a Dictionary object.

The following code illustrates how to create a Dictionary object:


Dim d   'Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"   ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

If d.Exists("c") Then
    msg = "Specified key exists."
Else
   msg = "Specified key doesn't exist."
End If

a = d.Items   ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
   s = s & a(i) & "<BR>" ' Create return string.
Next

b = d.Keys   ' Get the keys.
For j = 0 To d.Count -1 ' Iterate the array.
    t = t & b(j) & "<BR>" ' Return results.
Next

d.Remove("b")   ' Remove second pair.
c = d.RemoveAll   ' Clear the dictionary.