+ Reply to Thread
Results 1 to 3 of 3

Thread: VB.NET from beginner to advanced programmer Part 16 - Multidimensional arrays

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    VB.NET from beginner to advanced programmer Part 16 - Multidimensional arrays

    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
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. 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:

    Code:
    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:

    Code:
    Dim anExample(4, 3) As Integer
    anExample(0, 3) = 5
    MessageBox.Show(anExample(0, 3))
    Note that you also can declare empty multidimensional arrays:
    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:
    Code:
    Dim anExample(,) As Integer
    ReDim anExample(3, 3)
    and this too:
    Code:
    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:
    Code:
    Dim anExample(,) As Integer
    ReDim Preserve anExample(3, 3)
    But not this:
    Code:
    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:

    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:

    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)
    will give us the output:

    Code:
    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.

    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 Integer
    Code:
    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: VB.NET from beginner to advanced programmer Part 16 - Multidimensional arrays

    Ah, here it is! Nicely done.
    +rep

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: VB.NET from beginner to advanced programmer Part 16 - Multidimensional arrays

    Thanks, what do you think of the last array? Is it something you would've used?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  2. Replies: 2
    Last Post: 11-02-2009, 06:01 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  4. VB.NET from beginner to advanced programmer Part 10 - Arrays
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 11-02-2009, 05:45 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts