ASPFAQs.com
Your source for ASP-related frequently asked questions and answers!


4GuysFromRolla.com : ASP FAQS : Arrays

Question: How do I put an array into a Session or Application "variable"? How do I then use it on the next ASP page?

Answer: First, my standard little lecture:

"Session Variable" and "Application Variable" are horrible misnomers. If you look at the ASP docs, you will see that what is actually in place is a collection named Session.Contents or Application.Contents. You are actually putting key/value pairs into this collection and then retrieving the values via the keys. So "Session Values" and "Application Values" might be better names. But we are stuck with the names MS gave this capability, forever no doubt.

Anyway...

Usually, the whole point of putting an array into a session or application value is that you want to be able to add more info to it in a later page. But even if you will not change the size of your array, the techniques described here work and work well.

The code that follows shows working with a "session array" that is intended to hold the contents of a user's shopping cart. The array is five (5) elements "wide" by any number of items "long." That is, for each item in the shopping cart, we will keep track of five pieces of information (the five "columns" of the array). And we won't set any limit on the number of items we will put in the cart.

We hope this kind of usage is fairly typical of the reason most coders would use a "session array."

On to the code. On the originating page (which could even be in Session_onStart in "global.asa", but certainly doesn't need to be):

<%
CONST CART_COLUMNS = 4
Dim cartArray( ) ' <<<< notice no size!
ReDim cartArray( CART_COLUMNS, 10 ) ' an arbitrary initial size
cartMaxUsed = -1 ' no elements used yet...
...
Session("cartArray") = cartArray ' names don't have to be same,
Session("cartMaxUsed") = cartMaxUsed ' but I like it that way
%>


Then, on any subsequent ASP page, we get the data from the Session values back into variables, manipulate the data as we want, and store the modified data back into the session values. Thus:

<%
Dim cartArray ' no parens! no size!
Dim cartMaxUsed

cartArray = Session("cartArray")
cartMaxUsed = Session("cartMaxUsed")
...
... work with the array ...
... add or change info ...
...
Session("cartArray") = cartArray ' names don't have to be same,
Session("cartMaxUsed") = cartMaxUsed ' but I like it that way
%>

But the key to making this useful is being able to ADD elements to the array. And you do that (in any page, including the first!) like this:

<%
' this function will return the *index* of the next
' available slot in the cartArray. If the cartArray
' is full, it is expanded before the index is returned!
'
Function addCartElement( )
    If cartMaxUsed >= UBound(cartArray,2) Then
        ' add 10 elements!
        ReDim Preserve cartArray( CART_COLUMNS, cartMaxUsed + 10 )
    End If
    cartMaxUsed = cartMaxUsed + 1
    addCartElement = cartMaxUsed
End Function
%>


And now you can use that function thus:

<%
...
' let's say we want to add an item to the shopping cart:
'
cartItem = addCartElement( )
cartArray(0,cartItem) = "Baby Blanket"
cartArray(1,cartItem) = "Blue"
cartArray(2,cartItem) = "Large"
cartArray(3,cartItem) = 14.95
cartArray(4,cartItem) = 2
'
...
%>

And we just added 2 large blue baby blankets at $14.95 each to the shopping cart.

Note that the "cart item number" must be the last array index. That's because ReDim Preserve can only change the last bound of an array.

Also, be careful to use the columns in the array consistently. You might even want to do something like this:

<%
CONST ITEM_NAME = 0
CONST ITEM_COLOR = 1
CONST ITEM_SIZE = 2
CONST ITEM_PRICE = 3
CONST ITEM_QUANTITY = 4
...
' let's say we want to add an item to the shopping cart:
'
cartItem = addCartElement( )
cartArray(ITEM_NAME,cartItem) = "Baby Blanket"
cartArray(ITEM_COLOR,cartItem) = "Blue"
cartArray(ITEM_SIZE,cartItem) = "Large"
cartArray(ITEM_PRICE,cartItem) = 14.95
cartArray(ITEM_QUANTITY,cartItem) = 2
'
...
%>


Which, just incidentally, makes the code much more readable.

Hints for performance and safety!

(1) Never work with an array "directly" in a session/application value. That is, never do something like Session("cartArray")(3,17) = "whatever". You should always copy the array to a VBS variable, use it and, if you alter it, copy it back to the session/application value.

(2) When using Application-level arrays, be sure to do Application.Lock before you read the value into the local VBS variable and then do *not* do Application.Unlock until after you have copied the altered value back to the application variable. (If you will only read the application value and not alter it, the Lock/Unlock is not strictly necessary. However, if you need to read multiple application variable values that should stay "in sync", then you should still use Lock/Unlock.)



FAQ posted by Bill Wilkinson at 10/16/2000 8:36:57 PM to the Arrays category. This FAQ has been viewed 108,962 times.


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers