Zebra0.com

visual-basic arrays

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",
   "April", "May", "June", "July", "August", 
   "September", "November", "December"}

'Illustrate arrays
Public Class Form1
    'Programmer: Janet Joy
    Dim English(10) As String 'global
    Dim Spanish As String() = {"cero", "uno", "dos", "tres", "cuatro", "cinco",
   "seis", "siete", "ocho", "nueve", "diez"}
    Private Sub Form1_Load(sender As Object, e As 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

    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        'Draws the word in both English and Spanish
        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: Example 1: Initialize