Example 1: Initialize

There is an easier way to initialize an array. We will add the Spanish words to illustrate. Notice that when you give a list you can not specify the size. Visual Basic will determine the size from the number of items in the list. The first item in the list is always (0) so we must include it:

Dim Months() As String = {"", "January", "February", "March",
   "September", "November", "December"}

'Illustrate arrays
Public Class Form1
    Dim English(10) As String 'global
    Dim Spanish As String() = {"cero", "uno", "dos", "tres", "cuatro", "cinco",
   "seis", "siete", "ocho", "nueve", "diez"}
        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

    Private Sub Form1_MouseClick(sender As Object, e As 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)
            Me.CreateGraphics.DrawString(Spanish(N), Me.Font, MyBrush, 80, N * 15)
        Next N
    End Sub
End Class
Experiment: Add column headings to the display. Declare English the same way that Spanish is declared and get rid of form load.

Next Page: Out of Bounds Exception

This lesson: Arrays in Visual Basic, Scalar Variables vs. Arrays, Example 1: Initialize, Out of Bounds Exception, Arrays of Objects, A lookup example, Julian Date, ReDim: Change the Size of the Array, No Duplicates, Collections