Discovering Arrays, Page 10
So far, we've talked about arrays with one single dimension. In essence, they're just a hyped-up list of values.
We can change the number of elements in such arrays, alter the values of individual items, even zap it all.
However there's something else you can do with 'em... add new dimensions.
Now, if I didn't loathe Star Wars so, you'd probably be the victim of a few futuristic dimension-related Garlick jokes right now. Or is it Dahlick? And how do you spell it? I forget.
Still, 'multiple dimensions' sounds terribly futuristic and scientific so I'm definitely up for it. Anything for a new buzzword.
Top Tip: This section is optional and included for completeness you see, erm, it's kinda unlikely you'll actually ever use the techniques demonstrated. So if you're in a hurry, feel free to skip the rest of this page!
Now, let's imagine we've declared our array like this:
Dim NDArray(1 To 5)
In visual terms, our array looks something like this:
We can also add another dimension to our arrays, by declaring them as such: Dim NDArray(1 To 5, 1 To 5)
In visual terms, our two-dimensional array ends up looking something like this a block of twenty-five individual elements:
So if we wanted to insert a value into the very first 'cell', we would use something like: NDArray(1,1)= "Test"
And to insert into the last cell: NDArray(5,5)= "Test"
You can even expand arrays out into three dimensions - though it's a little more difficult to draw. You do it in much the same way as before, and can imagine it rather like one big block containing many tiny little blocks, like those cubes you used to play with in math class: NDArray(1 To 5, 1 To 5, 1 To 5)
Incidentally, that's where three-dimensional (3D) gets its name from. Most television screens are two-dimensional, meaning you see just height and width. But with 3D films or images you get to see height, width and depth, as in this (rather poorly drawn) visual representation of our three-dimensional array.
You can even expand arrays out into four and five dimensions measured in scientific terms of time and space, I think but they're awfully difficult to draw.
But to be completely honest, unless you really need such whopping great arrays for mathematical purposes or for creating some kind of highly complex, custom-built, multi-dimensional data warehousing type-application, you really don't need to dive past a simple one-dimensional array.
You'll find arrays of two-dimensions pretty rare, and chances of coming across a three-dimensional array are about the same as spotting the entire cast of Lion King merrily dancing to the tune of Hi Lili Hi Lo in the Highlands of Bonny Scotland.
Still, just to prove it can be done, let's create a sample two-dimensional array project.
- Create a new Standard EXE
- Declare the following dynamic array in General Declarations:
Dim NDArray() As Integer
Now, we're going to tell Visual Basic to redimension this array to hold two dimensions, one containing elements one to five, the second also containing elements one to five. In other words, we're going to create ourselves a virtual 2D grid similar to the one I drew above.
- Add a Command Button (Caption: Redim 2D Array), then insert the following code behind it:
Private Sub Command1_Click()ReDim NDArray(1 To 5, 1 To 5)End Sub
In this sample project, I'm going to create a multiplication table. The code may seem a bit strange at first, but once you see it all up and running, you'll understand what's happening.
- Add another Command button (Caption: Fill 2D Array), throwing the following code behind it:
Private Sub Command2_Click() For i = 1 To 5 For y = 1 To 5 NDArray(i, y) = i * y Next y Next iEnd Sub
This just loops around i five times. Within each loop of i, a sub loop goes around y five times also. The value of i * y is then inserted into the (i, y) location of NDArray.
Seems weird, don't it? Try running the code in your own head first. Go through it all line by line. What results do you get? Do you see how it works? If not, don't worry it'll become clear later.
Next up, we're going to add another form with a grid, on which we will plot the data from our array.
- Add another Form to your project
Now let's add a reference to the Microsoft FlexGrid control:
- Click Project, Components
- Scroll down, tick 'Microsoft FlexGrid Control', then click OK
- Draw out the MSFlexGrid control onto your new Form
- Change the following properties of your MSFlexGrid, observing how the grid alters as you do so:
Rows - 5
Cols - 5
FixedRows - 0
FixedCols - 0
Great! We've just prepared the second form to display the contents of our two-dimensional array, which in its simplest form, is a five-by-five grid.
- Add another Command Button (Caption: Fill Grid) to your first Form
Now, in the code we're going to use to put data into that grid, I'll be using the LBound and UBound functions to determine the lower and upper boundaries of the array, a lot like we did before.
However, remember that LBound returns the first element of an array, whilst UBound returns the last? Well, that was with a simple one-dimensional array. Now we're working with two dimensions!
So it's worth noting that both LBound and UBound have one extra optional argument. After passing the array to the function, you can add a number to indicate which dimension you're referring to. If no number is entered, the first dimension is assumed.
That means LBound(NDArray, 1) returns the lowest element number of the first dimension in our case, one. And UBound(NDArray, 2) returns the highest element number of the second dimension in our case, five.
It's a small detail, but definitely worth highlighting.
Note: For more information about this, lookup LBound or UBound in Visual Basic help.
- Insert this code behind the button:
Private Sub Command3_Click() Form2.Show For i = LBound(NDArray, 1) To UBound(NDArray, 1) ' Translates into: For i = 1 to 5 For y = LBound(NDArray, 2) To UBound(NDArray, 2) ' Translates into: For y = 1 to 5 Form2.MSFlexGrid1.TextMatrix(i - 1, y - 1) = NDArray(i, y) ' Throws the (i, y) array value into the grid Next y Next i End Sub
This code simply cycles round the array and dumps the information into our grid.
Top Tip: The TextMatrix bit specifies where in the grid the information should be placed. If you're wondering why we take one from i and y to specify the location of the grid cell you want to use... well, it would seem the grid uses that horrid zero-based counting system too so the first cell is (0,0), not (1,1). That's why we need to minus one from our array, which uses a one-based counting system. Sheesh! <Karl shivers>
And the resulting form should look something like this:
There we have it: our multiplication table!
If I wanted to know four times five, I simply need to look down the number five column, then look across the number four row and see where they cross over. That's where the answer will lie.
Not terribly useful, but a cool party trick if ever I saw one...
Briefly, let's review the important points in this section:
- Arrays can have multiple dimensions, though it isn't very common
- To retrieve the LBound and UBound of certain dimensions within an array, use this format: LBound(ArrayName, DimensionNumber)
Top Tip: If you're going to be working with multi-dimensional arrays and want to use the Preserve command too, be sure to check out the Visual Basic help file first, as special rules apply!
< Ed: You can download the code for this project here. >