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 properties and methods of the ASP Dictionary object are as follows:
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.
<%@ LANGUAGE="VBSCRIPT" %>
'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
|Copyright © 2005-2007 www.WebCheatSheet.com All Rights Reserved.|