Dictionary object
Dictionary objects are used to store information in name/value pairs (equivalent to keys and items). Dictionary objects appear to be simpler than arrays, however, Dictionary objects are a more satisfactory solution for handling related data.
Compare Dictionary and array:
Keys are used to identify items in a Dictionary object.
There is no need to call ReDim to change the dimensions of the Dictionary object.
When an item is deleted from a Dictionary, the remaining items are automatically moved up
. Dictionary is not multi-dimensional, whereas arrays are
. Dictionary has more compared to arrays. The built-in object
Dictionary works better than arrays when accessing random elements frequently.
Dictionary works better than arrays when locating items based on their contents.
The properties and methods of the Dictionary object are described as follows:
Properties
CompareMode: Sets or returns the comparison mode used to compare keys in Dictionary objects.
Count: Returns the number of key/item pairs in the Dictionary object.
Item: Sets or returns the value of an item in the Dictionary object.
Key: Set a new key value for the existing key value in the Dictionary object.
method
Add: Adds a new key/item pair to the Dictionary object.
Exists: Returns a logical value that indicates whether the specified key exists in the Dictionary object.
Items: Returns an array of all items in the Dictionary object.
Keys: Returns an array of all keys in the Dictionary object.
Remove: Removes the specified key/item pair from the Dictionary object.
RemoveAll: Removes all key/item pairs in the Dictionary object.
program code
<%
Dim oDic,aItems,aKeys
Set oDic = Server.CreateObject("Scripting.Dictionary")
'Add
oDic.Add "aaa",111
oDic.Add "bbb",222
oDic.Add "ccc",333
oDic.Add "ddd",444
'Modify
If oDic.Exists("aaa") Then
oDic.key("aaa") = "eee" 'key attribute, read-only
oDic.item("eee") = 555 'item attribute, readable and writable
End If
'List
aKeys = oDic.Keys
aItems = oDic.Items
For i=0 To oDic.Count-1
Response.Write(aKeys(i) & "," & aItems(i))
Next
'Delete
Response.Write(oDic.Count)
oDic.Remove("eee") 'Remove key and item pairs from Dictionary object
Response.Write(oDic.Count)
oDic.RemoveAll() 'Remove all key and item pairs in the Dictionary object
Response.Write(oDic.Count)
Set oDic = Nothing
%>