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:
* All pre-defined collections of the
Request object are read only.
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.Alternatively, an index and the Count, Keys, and Items methods may be used:
Key is a variable name we choose.
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:How to Define a Collection
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
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"
List.Remove("Tuesday")
List.Key("Saturday") = "Sabado"
List.Item("Sunday") = "Vegetables"
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. |
| 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:
|
| 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. |