ASP DictionaryObject
The Dictionary object is used to store information in name/value pairs.
Try it Yourself - Examples
Does the specified key exist?
This example demonstrates how to create a Dictionary object, and then use the Exists method to check whether a 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/item pairs
This example demonstrates how to use the Count property to return the number of key/item pairs.
Dictionary Object
The Dictionary object is used to store information in name/value pairs (equivalent to keys and items). The Dictionary object may seem simpler than an array; however, the Dictionary object is a more satisfactory solution for handling associated data.
Compare Dictionaries and arrays:
- Keys are used to identify items in the Dictionary object
- You do not need to call ReDim to change the size of the Dictionary object
- When an item is deleted from the Dictionary, the remaining items automatically move up
- Dictionaries are not multidimensional, while arrays are multidimensional
- Dictionaries have more built-in functions than arrays
- Dictionaries work better than arrays when frequently accessing random elements
- Dictionaries work better than arrays when locating items based on their content
The following example creates a Dictionary object, adds some key/item pairs to the object, and then retrieves the item value of the key gr:
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 gr is: " & d.Item("gr"))
%>
Output:
The value of key gr is: Green
The properties and methods of the Dictionary object are described below:
Properties
| Property | Description |
|---|---|
| CompareMode | Sets or returns the comparison mode used to compare keys in the Dictionary object. |
| 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 | Sets a new key value for an existing key in the Dictionary object. |
Methods
| Method | Description |
|---|---|
| Add | Adds a new key/item pair to the Dictionary object. |
| Exists | Returns a Boolean 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 from the Dictionary object. |
Other Extensions