Bookmark and Share

ASP Arrays

Arrays in ASP follow the exact same form and rules as those arrays in VBScript. You can create an array of specific size or you can create a dynamic sized array. Below we have examples of both types of arrays.

Advertise on Tizag.com

ASP Code:

<%
Dim myFixedArray(3) 'Fixed size array
Dim myDynArray() 'Dynamic size array

%>

We are going to focus on fixed size arrays first and cover dynamic arrays later on in this lesson.

Assigning Values to an Array

Let's fill up our fixed size array with values. Our fixed array is going to store the names of famous people. To assign a value to an array you need to know three things:

  • The name of the array
  • The value you want to store
  • The position in the array where you want to store the value.

An array is a group of variables that you can access by specifying the position inside the array. Our array myFixedArray has four positions: 0, 1, 2 and 3. Let's assign some values to our array.

ASP Code:

<%
Dim myFixedArray(3) 'Fixed size array
myFixedArray(0) = "Albert Einstein"
myFixedArray(1) = "Mother Teresa"
myFixedArray(2) = "Bill Gates"
myFixedArray(3) = "Martin Luther King Jr."
%>

ASP Arrays are Zero Based!

If you're a programmer you might look at that above example and thing "Hey, you only allocated three elements for that array, but you assigned four values! Well, this can be done because arrays in ASP are zero based. This means when you declare fixed array of size X then you can assign values for each value from 0 through X.

Below is perfectly functional ASP code that will go through our array and print out the contents of each element. Don't worry if you are confused by this new for loop code it will be taught later.

ASP Code:

<%
Dim myFixedArray(3) 'Fixed size array
myFixedArray(0) = "Albert Einstein"
myFixedArray(1) = "Mother Teresa"
myFixedArray(2) = "Bill Gates"
myFixedArray(3) = "Martin Luther King Jr."
For Each item In myFixedArray
	Response.Write(item & "<br />")
Next
%>

Display:

Albert Einstein Mother Teresa Bill Gates Martin Luther King Jr.

ASP Dynamic Sized Arrays

To create an array whose size can be changed at any time simply do not put a number within the parenthesis when you declare the array. When you know what size you want the array to be use the ReDim keyword. You may ReDim as many times as you wish.

If you want to keep your data that already exists in the array then use the Preserve keyword. Below is an example of all these things we have just talked about.

ASP Code:

<%
Dim myDynArray() 'Dynamic size array
ReDim myDynArray(1)
myDynArray(0) = "Albert Einstein"
myDynArray(1) = "Mother Teresa"
ReDim Preserve myDynArray(3)
myDynArray(2) = "Bill Gates"
myDynArray(3) = "Martin Luther King Jr."
For Each item In myDynArray
	Response.Write(item & "<br />")
Next
%>

Display:

Albert Einstein Mother Teresa Bill Gates Martin Luther King Jr.
Bookmark and Share




Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials