VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
Arrays with only one dimension was the only thing we learned about array earlier. Multidimensional arrays works pretty much the same but there's a few things you must know and think about when you use them.
To create an array with more then one dimension you just separate them with commas when declaring the array. Like this:
Dim anExample(4, 3) As Integer
So now we have declared an array with two arrays, one with the size 5 and one with the size 4. This means the array have 20(5*4) places to store values in.
To access the values you just use the same principle:
Dim anExample(4, 3) As Integer anExample(0, 3) = 5 MessageBox.Show(anExample(0, 3))
Note that you also can declare empty multidimensional arrays:
Dim anExample(,) As Integer
Redim
This will in some cases work a little bit different then with one dimensional arrays.
This will work as expected:
Dim anExample(,) As Integer ReDim anExample(3, 3)
and this too:
Dim anExample(2, 1) As Integer ReDim anExample(3, 3)
However when we use preserve to keep the values in the array we'll find a difference.
This still works:
Dim anExample(,) As Integer ReDim Preserve anExample(3, 3)
But not this:
Dim anExample(2, 1) As Integer ReDim Preserve anExample(3, 3)
The reason is that when you're using preserve when redimming an multidimensional array you can only change the size of the dimension to the far right. But as you saw above we can set all sizes that haven't been already set without any troubles. To correct the above code we change the first "3" to a "2" we won't change the size of that dimension and therefor it will work:
Dim anExample(2, 1) As Integer ReDim Preserve anExample(2, 3)
.GetUpperBound and .Length
.GetUpperBound will work the same as when you're using one dimensional arrays. Just remember to use the right index. .Length will still return the length of the array but remember that the length is now the size of the dimensions multiplied with each other. For example:
Dim anExample(9, 99) As Integer
MessageBox.Show("Dimension 1(index 0) has the size: " & anExample.GetUpperBound(0) + 1)
MessageBox.Show("Dimension 2(index 1) has the size: " & anExample.GetUpperBound(1) + 1)
MessageBox.Show("The size of the array: " & anExample.Length & "=" & anExample.GetUpperBound(0) + 1 & "*" & anExample.GetUpperBound(1) + 1)
will give us the output:
Dimension 1(index 0) has the size: 10 Dimension 2(index 1) has the size: 100 The size of the array: 1000=10*100
For each
When using for each on a multidimensional array you need to know which order it gets the item. This simple example show how it works, the numbers from 1 to 9 will be showed in the correct order.
Dim anExample(2, 2) As Integer
anExample(0, 0) = 1
anExample(0, 1) = 2
anExample(0, 2) = 3
anExample(1, 0) = 4
anExample(1, 1) = 5
anExample(1, 2) = 6
anExample(2, 0) = 7
anExample(2, 1) = 8
anExample(2, 2) = 9
For Each item In anExample
MessageBox.Show(item)
Next
More then two dimensions
I have always talked about multidimensional arrays instead of two dimensional arrays, the reason is simple, you can use three dimensions if you want or all the way up to 32 dimensions if you would like. We can only imagine 3 dimensions normally but that doesn't mean we can't create arrays with a huge number of dimensions. Be careful though, your arrays can easily take up a huge space of memory. For example if you have an array with dimensions with the sizes 10, 15, 20, 2 and 5 the array will have 10*15*20*2*5=30,000 elements. A few examples, you probably get the idea:
Dim anExample(2, 1, 3, 2) As Integer
Dim anExample(,,) As Integer ReDim anExample(1, 4, 6)
Dim anExample(5, 1, 3) As String anExample(1, 0, 2) = "Let's store this here"
Dim anExample(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31) As Integer
That was all about multidimensional arrays. Just remember, don't make them too big(the last example is actually too big, an error will occur). See you later. :)
Edited by Vswe, 21 March 2010 - 02:36 PM.


Sign In
Create Account


Back to top









