Dynamic Arrays

In our 'Arrays' tutorial we showed you how to create a fixed size array. The code below declares the array using the Dim statement and passes in a parameter which specifies the maximum number of items that the array can contain. In this case it's 7 as the array has a zero based starting index.

<%
Dim myArray(6)
%>

Dynamic arrays come in handy when you aren't sure how many items your array will hold. To declare a dynamic array simply leave out the parameter.

<%
Dim myArray()
%>

Suppose you wished to create an array to hold all the item ID's in your visitors shopping cart. As you aren't sure how many items they'll purchase you could first of all create a dynamic array.

<%
Dim myShopCartArray()
%>

If your visitor then puts 3 items in their cart you could resize the array using the REDIM function (redimension).

<%
Dim myShopCartArray()

Redim myShopCartArray(2) 'remember zero based index
%>

If the visitor puts another item in their shopping cart you'll need to resize the array again. However you'd lose all the information in the existing array if you just used the Redim function. Using the keyword PRESERVE will keep the array we already have and increase the size.

<%
Dim myShopCartArray()

Redim myShopCartArray(2) 'remember zero based index
Redim PRESERVE myShopCartArray(3)
%>


In part 1 read about 'Arrays'
In part 2 read about 'Array Functions'
In part 3 read about 'Multidimensional Arrays'

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Get your best asp web hosting provider now and save 25%