Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
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:
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.Code:Dim anExample(4, 3) As Integer
To access the values you just use the same principle:
Note that you also can declare empty multidimensional arrays:Code:Dim anExample(4, 3) As Integer anExample(0, 3) = 5 MessageBox.Show(anExample(0, 3))
Code: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:
and this too:Code:Dim anExample(,) As Integer ReDim anExample(3, 3)
However when we use preserve to keep the values in the array we'll find a difference.Code:Dim anExample(2, 1) As Integer ReDim anExample(3, 3)
This still works:
But not this:Code:Dim anExample(,) 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:Code:Dim anExample(2, 1) As Integer ReDim Preserve anExample(3, 3)
Code: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:
will give us the output:Code: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)
For eachCode:Dimension 1(index 0) has the size: 10 Dimension 2(index 1) has the size: 100 The size of the array: 1000=10*100
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.
Code: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:
Code:Dim anExample(2, 1, 3, 2) As IntegerCode:Dim anExample(,,) As Integer ReDim anExample(1, 4, 6)Code:Dim anExample(5, 1, 3) As String anExample(1, 0, 2) = "Let's store this here"Code: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.![]()
Last edited by Vswe; 03-21-2010 at 03:36 PM.
Ah, here it is! Nicely done.
+rep
Thanks, what do you think of the last array? Is it something you would've used?![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks