ASP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

"Dictionary" Class - Collection of Key-Value Pairs

This section provides a tutorial example on how to use the Dictionary class that represents a collection of pairs of keys. Dictionary class is provided in the Scripting Runtime DLL, scrrun.dll.

Dictionary: A class represents a collection of pairs of keys and values provided by the Scripting Runtime DLL, scrrun.dll. The Dictionary class offers the following methods and properties:

  • "Add key, item": Method to add a pair of key and value to this dictionary.
  • "Exists(key)": Method to return true if the specified key exists in this dictionary.
  • "Keys()": Method to return an array containing all the keys in this dictionary.
  • "Items()": Method to return an array containing all the values in this dictionary.
  • "Remove(key)": Method to remove a pair of key and value from this dictionary.
  • "RemoveAll()": Method to remove all pairs of keys and values from this dictionary.
  • "Item(key)": Property to set or return the value associated with the specified key.
  • "Count": Property to return the number of pairs of keys and values.

Here is a sample ASP page to show how to use "Dictionary" class:

<script language="vbscript" runat="server">
'  dictrionary_test.asp
'  Copyright (c) 1999 by Dr. Herong Yang
'  This program shows how to use the Dictionary class.
'
   response.write("<html><body>")
   response.write("<b>Tests on dictionary class</b>:<br/>")
   ' Creating a dictionary object
   set user = CreateObject("Scripting.Dictionary")
   ' Adding a pair of key and item
   user.add "FirstName", "Bill"
   user.add "LastName", "Smith"
   user.add "Email", "bill@com.com"
   user.add "Country", "Canada"
   ' Iterating through the dictionary
   n = user.Count
   response.write("Before - Number of keys: " & n & "<br/>")
   keys = user.Keys()
   for i=0 to n-1
      k = keys(i)
      v = user.Item(k)
      response.write(k & ": " & v & "<br/>")
   next
   ' Modifying the dictionary
   user.remove("Country")
   user.Item("Email") = "bill@smith.com"
   user.Item("Phone") = "123-456-7890"
   ' Iterating through the dictionary
   response.write("After - Number of keys: " & n & "<br/>")
   keys = user.Keys()
   for i=0 to n-1
      k = keys(i)
      v = user.Item(k)
      response.write(k & ": " & v & "<br/>")
   next
   response.write("</body></html>")
</script>

Output:

Tests on dictionary class:
Before - Number of keys: 4
FirstName: Bill
LastName: Smith
Email: bill@com.com
Country: Canada
After - Number of keys: 4
FirstName: Bill
LastName: Smith
Email: bill@smith.com
Phone: 123-456-7890

Note that:

  • "CreateObject()" method is called to instantiate an object out of the "dictionary" class.
  • Method with two parameters can not use parenthesis to list the parameters, for example, 'user.Add "FirstName", "Bill"'. This is a very strange syntax for experienced programmers, because all most every other language requires parenthesis, no matter how many parameters there are in a method call.
  • Property "Item(key)" can be used on the left hand side of an assignment operator, for example, 'user.Item("Email") = "bill@smith.com"'.
  • Property "Item(key)" used an assignment statement will force an auto-addition of the specified key to this dictionary, for example, 'user.Item("Phone") = "123-456-7890"'.

Last update: 2002.

Table of Contents

 About This Book

 ASP (Active Server Pages) Introduction

 IIS (Internet Information Services) 5.0

 MS Script Debugger

 VBScript Language

 ASP Built-in Run-time Objects

 ASP Session

 Creating and Managing Cookies

 Managing Sessions with and without Cookies

scrrun.dll - Scripting Runtime DLL

 What Is scrrun.dll?

"Dictionary" Class - Collection of Key-Value Pairs

 "FileSystemObject" Class - File System

 "Drive" Class - Disk Drive

 "Folder" Class - File Folder

 "File" Class - File Folder

 "TextStream" Class - Input or Output Stream

 Managing Response Header Lines

 Calculation Speed and Response Time

 ADO (ActiveX Data Object) DLL

 Working with MS Access Database

 Guest Book Application Example

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
"Dictionary" Class - Collection of Key-Value Pairs