Multi-Dimensional Arrays In ASP

This tip is mostly for new developers who don't have a great deal of experience working with arrays much less multi-dimensional arrays. Today, we'll cover a few of the basics including setting dynamic elements for the array.

This tip is mostly for new developers who don't have a great deal of experience working with arrays much less multi-dimensional arrays. Today, we'll cover a few of the basics including setting dynamic elements for the array.

<%

Sub TestArray(nElement2)

Dim sArray ' Declare your variant for the array
Dim nCur
Dim nNewTot

' Dimension the array into 2 dimensions

Redim sArray(2,nElement2)

For nCur = 0 to nElement2
sArray(0,nCur) = "some value " & nCur
sArray(1,nCur) = "some other value " & nCur
Next

' Get second dimensions Ubound

nNewTot = Ubound(sArray,2)

' Create a new total

nElement2 = clng(nElement2) * 2

' It is important to keep in mind that Redim Preserve
' only supports redimensioning the "right" most element
' of any multi-dimensional array. The Preserve keyword
' allows us to keep the current contents in the array.

Redim Preserve sArray(2,nElement2)

' Notice that I did not reuse Redim Preserve
' multiple times in the loop to dynamically
' make the array larger as needed. This is a
' serious resource problem so it should be avoided
' at all costs. Making an array much bigger
' than it will ever be and THEN Redim Preserve
' upon completion is a much more efficient way if
' you are unable to properly dimension the array
' upfront.

For nCur = nNewTot To nElement2
sArray(0,nCur) = "some value " & nCur
sArray(1,nCur) = "some other value " & nCur
Next

End Sub

TestArray 10

%>

Hope this helps you when working with arrays in the
future.


Submission Date:  9/23/2005 3:01:30 PM
Submitted By:  Robbe Morris
My Home Page:  http://www.robbemorris.com

By Robbe Morris   Popularity  (1508 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP