Jump to content

VB.NET from beginner to advanced programmer Part 10 - Arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
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





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:

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

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:


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:


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:

        ReDim Preserve anExample(10)


That was all for this Part. In the next part we will learn about For loops and For Each loops.

Edited by Vswe, 21 March 2010 - 02:31 PM.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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!

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Yes, I thought it was better to wait with them :)