ASP Lecture Series (8) Using Collections
Author:Eve Cole
Update Time:2009-05-30 19:59:11
Most ASP built-in objects support collections. Collections are where strings, numbers, objects, and other values are stored. Collections are very similar to arrays, except that the collection automatically expands and searches when items are stored or retrieved. Unlike arrays, when a collection is modified, the position of the items will move. Items can be accessed by their name, index, or by traversing through all items in the collection.
Accessing items by name and index You can access specific items in a collection by using the item name. For example, the Contents collection holds all the variables stored in the Session object. Also owns all objects created by Server.CreateObject. Assume that the following user information is stored in the Session object:
<%
Session.Contents("FirstName") = "Sam"
Session.Contents("LastName") = "Woo"
Session.Contents("Age") = 29
%>
Items can be accessed using the name associated with the item when it was stored in the collection. For example, the following expression returns the string "Sam":
<%= Session.Contents("FirstName") %>
Items can also be accessed by using the index or number associated with the item. For example, the following expression retrieves the information stored in the second storage slot of the Session object and returns "Woo":
<%= Session.Contents(2) %>
ASP collections are numbered starting from 1. When items are added or removed from the collection, the index associated with the item will change. So it cannot be assumed that the index of the item remains unchanged. As will be explained in the following topics, access using indexes is generally used to traverse a collection, or to access items in a read-only collection.
Projects are accessed by using their shorthand names. ASP searches the collections associated with objects in a specific order. If an item of a specific name occurs only once in a collection of objects, you can eliminate the name of the collection:
<%= Session("FirstName") %>
When accessing items stored in Application or Session objects, it is generally safe to eliminate the collection name. However, for Request objects, it is better to specify the collection name because the collection is likely to contain duplicate names.
Traversing a Collection Traverse through all items of a collection to learn about the items stored in the collection or to modify items. When traversing a collection, a collection name must be provided. For example, you can use the For...Each statement in VBScript to access items stored in a Session object:
<%
'Declare a counter variable.
Dim Item
'For each item in the collection, display its value.
For Each Item in Session.Contents
Response.Write Session.Contents(Item) & "<BR>"
Next
%>
You can use the For...Next statement in VBScript to iterate through a collection. For example, to list the three items stored in the Session in the above example, you would use the following statement.
<%
'Declare a counter variable.
Dim Item
'Repeat the loop until the value of counter is equal to 3.
For Item = 1 to 3
Response.Write Session.Contents(Item) & "<BR>"
Next
%>
Because the number of items stored in a collection is generally not known, ASP supports the Count property of collections, which returns the number of items in the collection. You can use the Count property to specify the final value of the counter.
<%
'Declare a counter variable.
Dim Item
'Repeat this loop until the counter equals the number of items
'in the collection.
For Item = 1 to Session.Contents.Count
Response.Write Session.Contents(Item) & "<BR>"
Next
%>
You can use the for statement in a script to loop through a collection. When using the Count property in a JScript for statement, in order to achieve greater results, you should assign the Count value to a local variable and use that variable to set the final counter value. In this way, the script engine does not need to look up the value of Count every time it loops. The following example demonstrates this technique:
<%
var item, numitems;
numitems = Session.Contents.Count;
for (item = 1; item <= numitems; item++) {
Response.Write(Session.Contents(item) + "<BR>")
}
%>
Microsoft JScript 3.0 introduced the Enumerator object. You can use this object to traverse ASP collections. The atEnd method indicates whether there are still items in the collection. The moveNext method moves to the next item in the collection.
<%
// Create an Enumerator object
var mycoll = new Enumerator(Session.Contents);
//Iterate through the collection and display each item
while (!mycoll.atEnd()) {
var x = mycoll.item();
Response.Write(Session.Contents(x) + "<BR>");
mycoll.moveNext();
}
%>
The script that iterates through a collection of subkeys embeds relevant values in a single cookie to reduce the number of cookies sent between the browser and the Web server. Therefore the Cookies collection of Request and Response objects can have multiple values in a single item. These sub-items or sub-keywords can be accessed individually. Only the Request.Cookies and Response.Cookies collections support subkeys (Subkeys). Request.Cookies only supports read operations; Response.Cookies only supports write operations.
You can enumerate all cookies in the Request.Cookie collection and all subkeys (Subkeys) in the Cookie. However, iterating over subkeys on a cookie without subkeys will produce no results. You can avoid this by using the .HasKeys syntax to first check whether the cookie contains subkeys. The example below demonstrates this technique.
<%
'Declare counter variables
Dim Cookie, Subkey
'Display the entire cookie collection.
For Each Cookie in Request.Cookies
Response.Write Cookie & "<BR>"
If Request.Cookies(Cookie).HasKeys Then
'Display the subkeys
For Each Subkey in Request.Cookies(Cookie)
Response.Write Subkey & "=" & Request.Cookies(Cookie)(Subkey) & "<BR>"
Next
Else
Response.Write "No subkeys in this cookie <BR>"
End If
Next
%>
Iterate over a collection of objects
Session and Application collections can hold quantitative variables or object instances. The Contents collection holds quantity variables and object instances generated by calling Server.CreateObject. The StaticObjects collection holds objects created using the HTML <OBJECT> element in the Global.asa file.
When iterating over a collection containing an object, you can access the object's identifier or the object's methods and properties. For example, suppose your application uses a number of objects to create user accounts, and each object has an initialization method. You can iterate over the StaticObjects collection to call each initialization method:
<%
For Each Object in Session.StaticObjects
Session.StaticObjects(Object).InitializeUser
Next
%>
How are ASP collections different?
Although the ASP collections discussed in this topic are very similar to Visual Basic's Collection objects, there are some differences. ASP collections support the Count property and Item method, but not the Add and Remove methods.