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
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:
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.Code:<scope> <array_name>([<size>]) As <Type>
Here below is an example on how you declare an array with 5 elements:
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.Code:Dim anExample(4) As Integer
This example below show how to both get and set the value of the elements in an array:
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.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))
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:
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.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")
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.
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!
Yes, I thought it was better to wait with them![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks