Arrays in Visual Basic and classic ASP

For the programmer who is used to C-like syntax, working with arrays in Visual Basic or classic ASP can be aggravating.

In this post, I will briefly go over declaring single- and multi-dimensional arrays, then iterating through them — the basic operations that make arrays useful.

One-dimensional arrays


Let's declare an array with six elements.
Dim OneDimArray(5)

Yes, that says "5", but it has six elements. When we're going through the elements of this array, we'll start counting from zero and end at five.

Iterating through one-dimensional arrays


For i = 0 to UBound(OneDimArray)
Response.Write(i)
Next

There will be six elements iterated through.

General notes about arrays in Visual Basic


So far, we're left with the impression that Visual Basic is a strange language. When we declare arrays in VB, the real size is the declared array size plus 1.

If you're used to programming in a C-like programming language such as C++ or Java, it's the declared array size, period — although you still start counting at zero. The following would give you a five-element array of integers in C++ and Java.
int one_dim_array[5];

You would access the elements of this C++/Java array with one_dim_array[i] where i = 0,1,...,4. Accessing it with index 5 would take you outside the bounds of the array.

In Visual Basic, however, you get a six element array when you declare the following.
Dim OneDimArray(5)

You can access OneDimArray(i) with i = 0,1,...,5.

Multi-dimensional arrays


The following is a declaration of a two-dimensional array.
Dim TwoDimArray(4,2)

This declaration would give us an array with five rows and three columns.

Here's how a three-dimensional array is declared.
Dim ThreeDimArray(5,6,7)

This gives us a 6 by 7 by 8 array. That's (5+1) by (6+1) by (7+1) because of Visual Basic's array syntax.

Finally, we'll generalize into the case of an n-dimensional array.
Dim EnDimArray(x_1, x_2, ..., x_n)

Iterating through multi-dimensional arrays


Here's how you'd iterate through a two-dimensional array.
For i = 0 to UBound(TwoDimArray)
For j = 0 to UBound(TwoDimArray, 2)
Response.Write(i & "," & j)
Next
Next

The major difference between iterating through this and iterating through a one-dimensional array is that we called UBound() with two arguments instead of one. This is so that UBound() knows which dimension to look up the upper bound for. In this case, our inner loop stops at the upper bound of the second dimension — that's why we specified the "2" there.

For three dimensions, the logic is similar.
For i = 0 to UBound(ThreeDimArray)
For j = 0 to UBound(ThreeDimArray, 2)
For k = 0 to UBound(ThreeDimArray, 3)
Response.Write(i & "," & j & "," & k)
Next
Next
Next

In the innermost loop, we specified that we needed to look up the upper bound for the third dimension.

Finally, the general form for iterating through an n-dimensional array's n dimensions.
For i_1 = 0 to UBound(EnDimArray)
For i_2 = 0 to UBound(EnDimArray, 2)
...
For i_n = 0 to UBound(EnDimArray, n)
Response.Write(i_1 & "," & ... & "," & i_n)
Next
...
Next
Next

Why we need UBound()


The UBound() function can be called with either one argument (just the array name), or with two arguments: the array name, and a number representing which dimension we want to count the upper bound on.

Without UBound(), we can't know the upper bound for the particular dimension we're looping through at the moment.

Comments

dramoth said…
Unfortunately, most VBScript and VB manuals tell you that arrays start at 1 and expect you to use 1 as the starting number.

It's been a long running joke amongst C developers that there is no zero in VB/ASP

Popular posts from this blog

JavaScript syntax highlighters

The costs of configurable settings in your web application