VB.NET Arrays

Level:
Level1

Virtually any modern programming language has the concept of arrays. An array is like a list of variables. It allows us to work with the entire list or each individual element in the list. VB.NET’s support for arrays is similar to VB6’s although VB.NET’s object oriented capabilities makes it even easier for us to work with arrays. This tutorial walks through the basics of Visual Basic array handling.

Download the source code for this tutorial: VB.NET Array Source Sample


Declaring and Initializing an Array

To declare an array we use the Dim statement just like we do with any variable in Visual Basic .NET. The difference for an array is that we use the open and closed parenthesis syntax ().

Dim intArray() As Integer

This declares a variable named intArray that will store an array of integers. At this point all we’ve done is declare the array we haven’t actually initialized it. We can initialize the array like this:

intArray = New Integer() {21, 45, 33, 100}

You can also initialize empty arrays of a specified size. In the example above we declared our array with 4 values already in it. We can also declare an array by its size and let VB.NET populate each element with its default value. For example:

Dim testArray(5) As Integer

This declares an array of size 5. That means it contains 5 elements - VB.NET automatically populates each of these elements with the value 0. So the code above is equivalent to this code:

Dim testArray = New Integer() {0, 0, 0, 0, 0}


Interacting with an Array

Now let’s display the array in a MessageBox. You might be tempted to try some code like this to do that:

MessageBox.Show(intArray.ToString())

If you do this you will be greeted with a Message Box that says: System.Int32[] – not very useful.

image

Instead lets display the values of one of the elements in the array. For example in order to display the value of the first item in the array we can write this code:

MessageBox.Show(intArray(0))

If you run this code you will see a Message Box with the value 21 being displayed.

image

This is the first element in our array. Notice in order to interact with one element of the array we use the open and closed parenthesis with an index value in between: (0). This is a common way that programming languages work with elements in an array (C++ for example uses the square brackets instead of parenthesis but it looks very similar – like this: intArray[0].

NOTE: For those of you coming from a classic Visual Basic background you might be used to arrays starting at index 1 instead of index 0. In the “normal” world it makes sense to start counting at 1, but in the computer world we almost always start counting at 0. VB.NET changed its indexing to start at 0 now to match other programming languages. Don’t let it bite you.

So now can you guess how we would access the last element in the array?

MessageBox.Show(intArray(3))

image

Along with getting the value of an element in an array you use the same syntax to set the value of an element in an array. For example if we want to replace our second elements value with 50 (instead of 45 which it is now) we would use the following code:

intArray(1) = 50
MessageBox.Show(intArray(1))

Notice we are setting the second element of the array to 50 (using the index of 1 since our indexes start at 0). When we display the value in our message box we do in fact see 50 on the screen.

image


What’s the point of arrays

You might be noticing that arrays are very similar to normal variables so the question is why do we use them? There are a few reasons. One obvious reason is that it’s a lot less code. It’s much nicer to simply declare and array of size 20 with a line of code like this:

Dim testArray(20) As Integer

Compared to declaring 20 individual variables var1, var2, var3 … var20.

Dim var1 As Integer
Dim var2 As Integer
Dim var3 As IntegerDim var20 As Integer

Another reason to use arrays is because we can interact with them as a whole. That is if I have an array of 20 elements called testArray, I can pass it into my test method with a simple line of code like this:

TestMethod(testArray)

If I didn’t have an array I’d have to pass 20 arguments into my test method.

One of the final reasons to use arrays is because they are easy to iterate over.


Iterating over arrays

So we have a list of integers. One of the most common things you will find yourself doing is iterating over each element in the array and doing something with it. For this example we will simply display each element of the array on the screen.

Dim intArray() As Integer
intArray = New Integer() {10, 20, 30, 40}
 
Dim counter As Integer
For counter = 0 To intArray.Count - 1
	MessageBox.Show(intArray(counter))
Next

When you run this code you will see 4 message boxes displayed one right after the other with the 4 values from our array. This is one of the most powerful things you can do with arrays. Arrays combined with loops allow us to easily perform calculations on each element of the array or across multiple elements in the array (for example to sort them).


Summary

Arrays are practically a necessity in programming. Any modern programming language needs to have full support for them and VB.NET doesn’t come up short in this area. As you begin to dig in deeper with arrays I’m sure you will find yourself using them extensively as you write Visual Basic .NET applications.

Download the source code for this tutorial: VB.NET Array Source Sample

If you enjoyed this post, subscribe for updates (it's free)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.