Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

ASP Arrays

An Array is an indexed list of items. By default the first item in the list starts with index 0. Shown below is the code for a list of the names of five colors.

<%
Dim arColors(5)

arColors(0)="Black" 
arColors(1)="Red" 
arColors(2)="Green" 
arColors(3)="Blue" 
arColors(4)="White" 
%>

The color name at index 0 is "Black", the color name at index 1 is "Red", and so on up to the color name at index 4 which is "White". If you find that starting a list with the index 0 is confusing, you can use the "Option Base 1" declaration at the top of the code to change the default starting index to 1.

<%
Option Base 1
Dim arColors(5)

arColors(1)="Black" 
arColors(2)="Red" 
arColors(3)="Green" 
arColors(5)="Blue" 
arColors(5)="White" 
%>

Then the color name at index 1 is "Black", the color name at index 2 is "Red", and so on. Another way to define the array is list the items, separated by commas, within parenthesis as shown below.

<%
Dim arColors
arColors = Array("Black","Red","Green","Blue","White")
%>

In that case you wouldn't need to indicate the length of the array in the Dim statement. However, by default, the first item in the list would start with index 0.

<%
Dim arColors(1 To 5)
arColors = Array("Black","Red","Green","Blue","White")
%>

Another way to set the starting index of an array is in the Dim statement as shown above. You would access the elements in an array using their index as shown below.

response.write arColors(3)

Assuning that you did not change the default starting index, the ststement above would write the color name "Blue".

<%
For i = 0 to 4
response.write arColors(i) & "<br>" 
Next
%>

You could write the entire list of colors using the For statement shown above.

Dynamic Arrays

There are two types of arrays in VBScript - static and dynamic. Static arrays have a fixed size. With a dynamic array, you can increase or decrease the number of items in the array. To create a dynamic array, do not specify the array's upper index in the Dim statement, as shown below.

<%
Dim arColors 
arColors = Array() 
%>

Then when you do decide how many elements the array will contain, you can use the Redim statement, as shown below, to size the array.

Redim arColors(5)

Note that in the Redim statement you use the number of elements in the array, not the highest index in the array.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Fire HD
[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyrightę2001-2010 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269