Monday 9 April 2012

Dictionary Object

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
Dim d   ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Atlas"   ' Add some keys and items.
d.Add "b", "Board"
d.Add "c", "Chart"
CompareMode Property
Sets and returns the comparison mode for comparing string keys in a Dictionary object.
compare
Optional. If provided, compare is a value representing the comparison mode. Acceptable values are 0 (Binary), 1 (Text), 2 (Database). Values greater than 2 can be used to refer to comparisons using specific Locale IDs (LCID).
Dim d
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
d.Add "a", "Atlas"   ' Add some keys and items.
d.Add "b", "Board"
d.Add "c", "Chart"
d.Add "B", "Ball"   ' Add method fails on this line because the 
                         ' letter b already exists in the Dictionary.
Item Property (Script Runtime)
Sets or returns an item for a specified key in a Dictionary object. For collections, returns an item based on the specified key. Read/write.
Function ItemDemo
   Dim d   ' Create some variables.
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "a", "Atlas"   ' Add some keys and items.
   d.Add "b", "Board"
   d.Add "c", "Chart"
   ItemDemo = d.Item("c")   ' Get the item.
End Function
Key Property
Sets a key in a Dictionary object.
Function DicDemo
   Dim d   ' Create some variables.
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "a", "Atlas"   ' Add some keys and items.
   d.Add "b", "Board"
   d.Add "c", "Chart"
   d.Key("c") = "d"   ' Set key for "c" to "d".
   DicDemo = d.Item("d")   ' Return associate item.
End Function
Add Method (Script Runtime)
Adds a key and item pair to a Dictionary object.
Dim d   ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Atlas"   ' Add some keys and items.
d.Add "b", "Board"
d.Add "c", "Chart"
Exists Method (Script Runtime)
Returns true if a specified key exists in the Dictionary object, false if it does not.
Function KeyExistsDemo
   Dim d, msg   ' Create some variables.
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "a", "Atlas"   ' Add some   keys and items.
   d.Add "b", "Board"
   d.Add "c", "Chart"
   If d.Exists("c") Then
      msg = "Specified key exists."
   Else
      msg = "Specified key doesn't exist."
   End If
   KeyExistsDemo = msg
End Function
Items Method
Returns an array containing all the items in a Dictionary object.
Function DicDemo
   Dim a, d, i, s   ' Create some variables.
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "a", "Atlas"   ' Add some keys and items.
   d.Add "b", "Board"
   d.Add "c", "Chart"
   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
   DicDemo = s
End Function
Keys Method
Returns an array containing all existing keys in a Dictionary object.
Function DicDemo
   Dim a, d, i   ' Create some variables.
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "a", "Atlas"   ' Add some keys and items.
   d.Add "b", "Board"
   d.Add "c", "Chart"
   a = d.Keys   ' Get the keys.
   For i = 0 To d.Count -1 ' Iterate the array.
      s = s & a(i) & "<br>" ' Return results.
   Next
   DicDemo = s
End Function
Remove Method (Script Runtime)
Removes a key, item pair from a Dictionary object.
Dim a, d   ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Atlas"   ' Add some keys and items.
d.Add "b", "Board"
d.Add "c", "Chart"
d.Remove("b")   ' Remove second pair.
RemoveAll Methods
The RemoveAll method removes all key, item pairs from a Dictionary object.
Dim a, d, i   ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Atlas"   ' Add some keys and items.
d.Add "b", "Board"
d.Add "c", "Chart"
a = d.RemoveAll   ' Clear the dictionary.

No comments:

Post a Comment