ASP Dictionary Object

The Dictionary object is used to store name/value pairs (referred to as the key and item respectively). The Dictionary object is like an associative array, but with its own built-in functionality that looks after the basic tasks of storing and manipulating the data, and sizing for the required number of elements. Each item in the Dictionary object is associated with a unique key. The key is used to retrieve an individual item. We don’t have to worry about which row or column the data is in, we just access it using a unique key.

There’re some rules you should keep in mind when you will use the Dictionary object:

  • The key is a unique identifier for the corresponding item, so it can not be used for any other item in the same Dictionary object. Having duplicate causes that the old value will be overwritten with the new one.
  • A value in a Dictionary object can not be an array. Dictionaries can not be multidimensional.

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


Properties Description
CompareMode The CompareMode property sets or returns the string comparison mode for comparing keys in the Dictionary object.
Count The Count is a read-only property that returns the number of key/item pairs in the Dictionary.
Item The Item property returns the value of the item for the specified key in the Dictionary object.
Key The Key property a new value for an existing key value in the Dictionary object.


Methods Description
Add The Add method adds the key/item pair to the Dictionary.
Exist The Exist property returns True if the specified key exists in the Dictionary object or False if not.
Items The Items method returns an array containing all the items in the Dictionary object.
Keys The Keys method returns an array containing all the keys in the Dictionary object.
Remove The Remove method removes one specified by key key/item pair from the Dictionary object.
RemoveAll Removes all the key/item pairs in the Dictionary object.

How to use the ASP Dictionary Object

In the following example we create an instance of the Dictionary object and add three key/item pairs to it. After that we retrieve the item value for the key1, remove the key2/item2 pair and change the value of a key and an item in the key1/item1 pair. Also this example demonstrates how to iterate through a dictionary and remove all the key/item pairs from it.

Dim objDict
'Create an instance of the Dictionary object
Set objDict = Server.CreateObject("Scripting.Dictionary")

'Add some key/item pairs to a Dictionary
objDict.Add "key1","item1" 'Add value item1 with key key1
objDict.Add "key2","item2" 'Add value item2 with key key2
objDict.Add "key3","item3" 'Add value item3 with key key3

'retrieve the item value for the "key1" and print it out
myItem = objDict.Item("key1")
Response.Write("The value corresponding to the key1 is " & myItem & "<br />")

'remove the pair key2/item2
myItem = objDict.Remove("key2")

'change the value of the item with the key "key1"
objDict.Item("key1") = "newValue"
Response.Write("The new value corresponding to the key1 is " & objDict.Item("key1") & "<br />")

'change the value of an existing key "key1" to "newKey",
'without changing the value of the corresponding item

objDict.Key("key1") = "newKey"
Response.Write("The value corresponding to the newKey is " & objDict.Item("newKey") & "<br />")

'retrieve all the keys and items from the Dictionary and print them out
allKeys = objDict.Keys   'Get all the keys into an array
allItems = objDict.Items 'Get all the items into an array

For i = 0 To objDict.Count - 1 'Iterate through the array
  myKey = allKeys(i)   'This is the key value
  myItem = allItems(i) 'This is the item value
  Response.Write("The " & i & " value in the Dictionary is " & myItem & "<br />")

'remove all the items

'free up resources
set objDict=nothing


Leave a Reply

Your email address will not be published.