+ Reply to Thread
Results 1 to 3 of 3

Thread: VB.NET from beginner to advanced programmer Part 10 - 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 10 - 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





    Here we now comes to the Arrays, a powerful way to store a lot of info. Arrays are variables that contains more then one value. To get or set a value of the array we need to refer to the index of the array. When we compared a variable with a box in Part 3, an array would have been a row of boxes. To get/set a value we need to know which of the boxes we want to handle with, we use the index for that.


    This is the syntax for declaring an array:

    Code:
     <scope> <array_name>([<size>]) As <Type>
    The syntax is the same as when declaring normal variables with the exception of the size of the array inside the parentheses. The size is how many elements(places to store values) there will be in the array. The size is not necessary even though the parentheses are. Without the size you can't referrer to any index of the array but you can set the size later.


    Here below is an example on how you declare an array with 5 elements:

    Code:
    Dim anExample(4) As Integer
    The name is starting with an "a" since its an array and then a "n" since it's an integer. The four means the highest index will be 4. Since the index starts at 0 this means we will get 5 elements.


    This example below show how to both get and set the value of the elements in an array:


    Code:
    Dim anExample(2) As Integer
    
    anExample(0) = 3
    anExample(1) = 5
    anExample(2) = 7
    
    MessageBox.Show("The element at index 1 in the array has the value " & anExample(1))
    In the example above you can see it's just to add the index in parentheses at the end and they work like normal variables.


    Using Redim and .GetUpperBound:

    As mentioned below you can change the size of an array, you do this with Redim. When using Redim you're not writing what type the array should be since it's using the same as it had. Note that all elements will be reseted in the array if you use Redim if you're not using Preserve. By using ".GetUpperBound(<dimensional>)" we gets a integer value which shows the element with the highest index's index in an array.

    Here's an example on how to use Redim and .GetUpperBound:


    Code:
    Dim anExample(0) As Integer
    
    MessageBox.Show("The array got " & anExample.GetUpperBound(0) + 1 & " elements")
    
    ReDim anExample(2)
    
    MessageBox.Show("The array got " & anExample.GetUpperBound(0) + 1 & " elements")
    
    ReDim anExample(99)
    
    MessageBox.Show("The array got " & anExample.GetUpperBound(0) + 1 & " elements")
    This "anExample.GetUpperBound(0) + 1", means we got the maximum index of the first dimensional(index 0). Since the index in the arrays also begins with 0 we use +1 to get the total numbers of elements. The output for this code will be(as a part of the string showed by a messagebox):1, 3 and 100.
    In this example we could also have used .Length. It's the same as .GetUpperBound + 1. So the only difference is that .Length return a value that is greater then 1 compared with using .GetUpperBound. But this ONLY works with one dimensional arrays, in multidimensional arrays it works completely different. More to them later on.


    To keep the values you had before you need to use Preserve, like so:

    Code:
            ReDim Preserve anExample(10)

    That was all for this Part. In the next part we will learn about For loops and For Each loops.
    Last edited by Vswe; 03-21-2010 at 03:31 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 10 - Arrays

    Arrays are powerful and very useful. I was going to ask where multidimensional arrays are, but I see you have a separate tutorial for that. +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 10 - Arrays

    Yes, I thought it was better to wait with them

+ 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: 2
    Last Post: 11-02-2009, 05:59 AM
  4. Replies: 1
    Last Post: 11-02-2009, 05:46 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