When a variable is declared with the statement Dim Num As Integer memory is allocated for one integer. If a list of numbers is needed, they can be declared as Num1, Num2, Num3, etc. However, an array is a much easier way to deal with a list of numbers. The statement Dim Num(4) As Integer declares a list of numbers that can be referred to as Num(0), Num(1), Num(2), Num(3), and Num(4). Notice that there are actually 5 numbers in the list because the first element in an array is always 0. The value inside the parenthesis is called the subscript, or index. One of the advantages of an array is that the subscript can be a variable. For example if we have an integer P with a value of 3, then Num(P) is the same as Num(3). For loops are often used to process an array.
The program below prints the words zero, one, two, three, … ten. The array is declared at the top to make it global. It is given initial values in form load. A for loop is used to print each element in the array, starting with 0.
Public Class Form1
Dim English(10) As String 'global
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
English(0) = "zero"
English(1) = "one"
English(2) = "two"
English(3) = "three"
English(4) = "four"
English(5) = "five"
English(6) = "six"
English(7) = "seven"
English(8) = "eight"
English(9) = "nine"
English(10) = "ten"
End Sub 'Form1_Load
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Dim MyBrush As New SolidBrush(Color.Black)
Dim N As Integer
For N = 0 To 10
Me.CreateGraphics.DrawString(N, Me.Font, MyBrush, 0, N * 15)
Me.CreateGraphics.DrawString(English(N), Me.Font, MyBrush, 15, N * 15)
Next N
End Sub 'Form1_MouseClick
End Class