ASP Collections

ASP pre-defines several collections.  In addition, we may define our own collections by instantiating instances of the "Scripting.Dictionary" object.

These are the pre-defined ASP Collections:

A collection is an object complete with data and methods.  Its data is organized as a list of values all having the same name (like an array) that are each distinguished by a string value (called a key) instead of an index.

If List were an array, its individual items might be List(0), List(1), List(2), etc.

But if List were a collection, we would not use 0,1,2, etc., to distinguish the items.  Instead we would use string values (called keys).  For example, List("Monday"), List("Tuesday"), List("Wednesday"), etc.

To reference a individual item of a collection, you must know the value of a key.

List("Tuesday")

To cycle (iterate) through the keys of a collection without knowing them, use a For Each loop:

For Each Key in List
    Response.Write Key & " has a value of " & List(Key) & "<br>"
Next

TheCollection is the name of the particular collection, either pre-defined or user-defined.
Key is a variable name we choose.
Alternatively, an index and the Count, Keys, and Items methods may be used:

For I = 0 to List.Count-1
    Response.Write List.Keys(I) & " has a value of " & List.Items(I) & "<br>"
Next

NOTE:  Keys and Items are special functions that retrieve the entire list of keys or items from the collection.  Therefore, the loop above is very inefficient because it calls the Keys and Items methods again and again, for each item in the collection.  If the list is long, this could take a long time and waste a lot of computing power.  A better version would call the Keys and Items methods only once each, storing their contents in variables and using the variables in the loop--like this:
KeyArray = List.Keys
ItemArray = List.Items
For I = 0 to List.Count-1
    Response.Write KeyArray(I) & " has a value of " & ItemArray(I) & "<br>"
Next
How to Define a Collection

We define our own collections by instantiated an instance of the "Scripting.Dictionary" object--like this:

Set List = CreateObject("Scripting.Dictionary")

Recall that Set is required anytime we instantiate an object (as opposed to simply assigning a value to a variable).
The line of code above creates a new List object whose data is empty.

How to Add Items to a Collection

List.Add "Monday", "Turkey"
List.Add "Tuesday", "Pork"
List.Add "Wednesday", "Beef"
List.Add "Thursday", "Ham"
List.Add "Friday", "Fish"
List.Add "Saturday", "Chicken"
List.Add "Sunday", "Mutton"

How to Delete Items from a Collection

List.Remove("Tuesday")

List.RemoveAll() How to Change an Item's Key without changing its Value

List.Key("Saturday") = "Sabado"

How to Change an Item's Value without changing its Key

List.Item("Sunday") = "Vegetables"

Summary of the Methods and Properties of the Dictionary Object

Methods

Method Description
Add key, item Adds the key/item pair to the Dictionary.
Exists(key) True if the specified key exists, False if not.
Items Returns an array containing all the items in a Dictionary object.
Keys Returns an array containing all the keys in a Dictionary object.
Remove(key) Removes a single key/item pair.
RemoveAll Removes all the key/item pairs.
Properties
Property Description
CompareMode Sets or returns the string comparison mode for the keys.  This property is not supported in Jscript.  VB constants may be used for the valid values as follows:
  • 0   vbBinaryCompare
  • 1   vbTextCompare
  • 2   vbDatabaseCompare
Count Returns the number of key/item pairs in the Dictionary.  Read only.
Item(key) Sets or returns the value of the item for the specified key.
Key(key) Sets or returns the value of a key.