When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Wednesday, October 28, 1998

WebDaily: Your daily source for Web Technology Tips and Tricks!

Utilizing the Dictionary Object in VBScript

* This article discusses using the Dictionary object in your ASP pages

One of Perl's nice features is what is called an associative array. An associative array differs from a "normal" array in one major way: rather than being indexed numerically (i.e. 0, 1, 2, 3, ...), it is indexed by a key, or an English-like word. ASP has something very similar to an associative array. This object is called the Dictionary object.

To construct an instance of a dictionary object, just use the following lines of code:

Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")

It's that simple. To add items to your dictionary object, all you need to do is use the Add method. The Add method takes two parameters, the key and the item. The key is similar to the index in a numerically-based, indexed array, and the item is the value at that index. Here is an example of using the add method:

objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"

Note that "Name" and "Age" are the keys and "Scott" and "20" are the items corresponding to those keys. To see if a key exists, just use the Exists method:

if objDictionary.Exists("Name") then
' Do something
' Do something else

end if

You can retrieve all of the items and keys using one of two methods. The Items method returns an array of all the items in the Dictionary object, and the Keys method returns an array of all the Keys in the Dictionary object.

Dim ItemsArray, KeysArray
ItemsArray = objDictionary.Items
KeysArray = objDictionary.Keys

' The # of Items = UBound(ItemsArray)
' You can traverse through the array to print out all the values

To get a specific item, use the Item property.
Dim MyName
MyName = objDictionary.Item("Name")
' MyName = "Scott"

You can use the Count property to get the number of keys / items in a Dictionary object (objDictionary.Count).

The last important methods are the Remove and RemoveAll methods. RemoveAll removes all of the key / index pairs, while the Remove takes a Key as a parameter and removes the key/item pair.


' Now the "Name" / "Scott" are no more


' Now the entire Dictionary object is empty

The Dictionary object is like Perl's associative array. It is not there to replace the array, but there are certainly times when it makes more sense to index your array using English-like terms as opposed to numerical values. Suppose, for instance, that you wanted to keep track of the current user's name, their referring URL, and their IP. You could make an instance of a Dictionary at the Session level in Session_OnStart, and populate it with the data from the Request.ServerVariables object. Then you could easily refer to your web visitor's information by objDictionary.Item("IP") or objDictionary.Item("RefURL") rather than using an array or three separate Session variables.

(For more information on the Request.ServerVariables object, please direct your browser to my article Using the Request.ServerVariables Object.)

Happy Programming!

To subscribe to WebDaily, point your browser to:

Please forward this information to a friend, including the subscription URL! Happy Programming!

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article