Visual Basic Arrays

After seeing how variables work, we have found that they only hold one value. Arrays, on the other hand, can hold more than one value, and they are common in large programs. They are similar to lists. When you create an array it is similar to a variable, but you just define the size of the elements like this:

Dim games (5)  As string

Arrays are accessed by their index number, which will be shown here. Create a new VB Console Application and name it Arrays.

Example

Dim Games(5) As String


Games(0) = "GTA 4"
Games(1) = "Battlefield 3"
Games(2) = "SWAT 4"
Games(3) = "Arma 2"
Games(4) = "RollerCoaster Tycoon 3"
Games(5) = "GRID"

Console.WriteLine(Games(0))
Console.ReadLine()

When you debug this (F5) you should see it print out GTA 4. First we make the array and then create that list, followed by the index number. Always remember, arrays start from 0.

User Input

For this example, we are going to get the input from the user.

Example

Dim Games(5) As String

Games(0) = "GTA4" 
Games(1) = "Battlefield 3" 
Games(2) = "SWAT 4" 
Games(3) = "Arma 2" 
Games(4) = "RollerCoaster Tycoon 3"
Games(5) = "GRID"

Dim x As Integer

x = 0 
Console.WriteLine("Pick a Number between 0 and 5") 
x = Console.ReadLine() 
Console.WriteLine(Games(x)) 
Console.ReadLine()

Code Explained

  • Here we created a integer variable named x
  • We gave x the value 0 (as a default value)
  • Next we asked the user to pick a number between 0 and 5
  • We then read that value using Console.ReadLine and stored it in x
  • Next we print it out using Console.WriteLine(Games(x))

For Each Loop

With arrays you can also loop through the array; in the following example we will print out all the values in the array Games.

 For Each GName As String In Games
     Console.WriteLine(GName)
Next
Console.ReadLine()

When you run this code you should get a list of the games; this is a very simple For Each Statement which we outlined before.

Going Backwards

You can also loop through the array backwards. To do this, we use the step keyword that we learned about before. Just like the For Each statement above, we simply add Array.Reverse(Games).

Example

Array.Reverse(Games)
For Each Gname As String In Games
    Console.WriteLine(Gname)
Next

Console.ReadLine()

This time the array will print out, but will be in reverse:

  • GRID
  • RollerCoaster Tycoon 3
  • Arma 2
  • SWAT 4
  • Battlefield 3
  • GTA 4

Sorting Arrays

You can also sort arrays alphabetically. The code is fairly simple; put the following code below Games (5) = "GRID":

Array.Sort(Games)

Comment out Array.Reverse(Games) in our previous For Each statement, and then debug the program. You should see:

  • Arma 2
  • Battlefield 3
  • GRID
  • RollerCoaster Tycoon 3
  • SWAT 4

Now the array is sorted alphabetically.

Summary

This tutorial covers the basics of arrays, which are are ideal for groups of data. We used the example of a list of favourite games. For a real life example see the  slideshow application.