Wednesday, 30 November 2011

Dictionary Method

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 a integer or a string, but can be anything except an array. The following code illustrates how to create a Dictionary object: [VBScript] 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” …
Methods
Add Method (Dictionary) | Exists Method | Items Method | Keys Method | Remove Method | RemoveAll Method
Add method : Adds a key and item pair to a Dictionary object Exists : Returns true if a specified key exists in the Dictionary object, false if it does not.
Items: Returns an array containing all the items of the dictionary object.
Keys: Returns an array containing all the keys of the dictionary object.
Remove: Remove the specified item from the dictionary object.
RemoveAll: Remove all items from the Dictionary object.
Count:Returns the number of items in a collection or Dictionary object. Read-only.
Item: Sets or returns an item for a specified key in a Dictionary object
Key: Sets a key in a Dictionary object.
Example: 
Function KeyExistsDemo
Dim d, msg ‘ Create some variables.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
d.Add “c”, “Cairo”
d.Remove(“c”)
d.RemoveAll
DicDemo = d.Item(“a”) ‘ Return associate item.
Exists
If d.Exists(“c”) Then
msg = “Specified key exists.”
Else
msg = “Specified key doesn’t exist.”
End If
KeyExistsDemo = msg
items: 

     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


Keys: 

    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

No comments:

Post a Comment