Interested in using arrays to help you organize your variables? Arrays can help you ‘group’ sets of information together, like the names of states, or names of countries, or colors, or any other grouping.
What is an array? An array is a variable that has multiple ‘buckets’ associated with it. Instead of creating variables called Book1, Book2, Book3, Book4, Book5, and so on, you simply make one array:
and that one array holds a group of values in one neat little package. This can be great for keeping together sets of information, or looping through ‘each book’ without having to write code to look at each variable one by one.
First off, note that arrays start with *0* and not with *1*. So if you use:
You then have access to:
Oceans(0) = ‘Indian’
Oceans(1) = ‘Pacific’
Oceans(2) = ‘Atlantic
And so on. You always begin with spot “0”.
If you start out with an array and waht to change its size later on, just use the Redim command. So you can say:
Note that arrays don’t need to have just one dimension (the ’32’ in our last example). You can have two dimensions, or up to 60 dimensions. Why would you want this? Well, let’s take our chessboard example. Say you wanted an array location to indicate each spot on a chessboard, so you could track pieces moving from one spot to another. You could do:
But then every time a piece moved you’d have to figure out where in that ’64’ it belonged. This could get tricky. What if instead you said:
Now it’s easy! Now you simply number the rows of the board from 1-8, and the columns of the board from 1-8, and change each number separately. If a Black Pawn moved from row 2, column 4 to row 3, column 4, you could say:
ChessSquares(2,4) = “EMPTY”
ChessSquares(3,4) = “BP”
Online ASP Course
ASP Array Function List