Thursday, February 8, 2007

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.

4 comments:

Mark 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

wangzi said...

This replica watch was the alone one of its affectionate that opened up like a fan. Men are cutting the A. Lange &Sohne ref. The Audemars Piguet Royal Oak Offshore in rose gold is awful approved afterwards and of advance Rolex continues to be popular, decidedly the 2-tone, animate and gold, dejected punch Rolex Submariner.

maher said...

replica watches swiss may be yours at a small price of the asking price of the original. The only folks who can tell amongst the original and then the chanel replica watches can be a jeweler and hubby may have to employ a jeweler's loop to do so. All the others will consider you are wearing the initial Breitling Different watches on the hand. Self-confidence will grow within you with this amazing timepiece bandaged around your wrist. The Porsche Design is just one of their hottest gps wirstwatches and epitomizes its accuracy in timekeeping. It is really a wonderful automatic chronograph stainless steel watch. You may collect the best watch on the market. Whenever your order is put most effective for you securely grouped together and shipped instantly. We've been operational for an extended time and cherish your affinity for our merchandise. The business transaction is just not comprehensive until that you are entirely content.

Ed said...

The C style lovers can use a special declaration at beginning each class or module file:

OPTION BASE ##

Indicates each array declared will be began with ## in your lower index.

User OPTION BASE 0 to do a similar C style functionality.

Examples

Option base 0
dim myarray(30) as string

myarray(0) = "some value"
myarray(1) = "some value"
myarray(2) = "some value"
...
myarray(29) = "some value"

With:
Option base 1
dim myarray(30) as string

myarray(1) = "some value"
myarray(2) = "some value"
...
myarray(30) = "some value"

Also you can define the starting index in the declaration:

dim myarrya(0 to 100) as string

Also the UBOUND, you can usae LBOUND to know the lower index.