ASP Dictionary Object

The Dictionary object is used to store information in paired name/value pairs (equivalent to keys and items).

Example

Does the specified key exist?
This example demonstrates how to first create a Dictionary object, and then use the Exists method to check if the specified key exists.
Return an array of all items
This example demonstrates how to use the Items method to return an array of all items.
Return an array of all keys
This example demonstrates how to use the Keys method to return an array of all keys.
Return the value of an item
This example demonstrates how to use the Item property to return the value of an item.
Set a key
This example demonstrates how to use the Key property to set a key in the Dictionary object.
Return the number of key/project pairs
This example demonstrates how to use the Count property to return the number of key/project pairs.

Dictionary object

The Dictionary object is used to store information in paired names/values (equivalent to keys and items). Although the Dictionary object may seem simpler than an array, it is a more satisfactory solution for processing associated data.

Comparison of Dictionary and array:

  • Keys are used to identify projects in the Dictionary object.
  • There is no need to call ReDim to change the size of the Dictionary object.
  • When a project is deleted from the Dictionary, the remaining projects are automatically moved up.
  • The Dictionary is not multi-dimensional, whereas an array is.
  • The Dictionary has more built-in objects than an array.
  • The Dictionary works better than an array when frequently accessing random elements.
  • The Dictionary works better than an array when locating items based on their content.

The following example creates a Dictionary object, adds some key/project pairs to it, and then retrieves the value of the key 'bl':

<%
Dim d
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add "re","Red"
d.Add "gr","Green"
d.Add "bl","Blue"
d.Add "pi","Pink"
Response.Write("The value of key 'bl' is: " & d.Item("bl"))
%>

Output:

The value of key 'bl' is: Blue

The properties and methods of the Dictionary object are described as follows:

Property

Property Description
CompareMode Sets or returns the comparison mode used to compare keys in the Dictionary object.
Count Returns the number of key/project pairs in the Dictionary object.
Item Sets or returns the value of a project in the Dictionary object.
Key Sets a new key/value for an existing key in the Dictionary object.

Method

Method Description
Add Adds a new key/project pair to the Dictionary object.
Exists Returns a logical value that indicates whether the specified key exists in the Dictionary object.
Items Return an array of all items in the Dictionary object.
Keys Return an array of all keys in the Dictionary object.
Remove Remove the specified key/project pair from the Dictionary object.
RemoveAll Remove all key/project pairs from the Dictionary object.