Lesson 96: Initializing Multidimensional Arrays

You can initialize the elements of a multidimensional array in several ways. Here are are just a few:

If you think about this list of ways to initialize tables, you will realize that you initialize tables and all other multidimensional arrays just as you do with any other variable. This method, however, allos you to think about your data in tabular form, which helps speed you programming and maintenance.

Suppose that a computer company sells two disk sizes: 3 1/2 and 5 1/4 inch. Each disk comes in one of four capacities. The disk inventory is well suited for a two-dimensional table. The disks have the following retail prices:

The following procedure stores the price of each disk in a table and prints the values to the form using a nested for loop.

 


Private Sub Form_Click()
Dim curDisks(1 To 2, 1 To 4) As Currency
Dim intRow As Integer, intCol As Integer

curDisks(1, 1) = 2.3
curDisks(1, 2) = 2.75
curDisks(1, 3) = 3.2
curDisks(1, 4) = 3.5
curDisks(2, 1) = 1.75
curDisks(2, 2) = 2.1
curDisks(2, 3) = 2.6
curDisks(2, 4) = 2.95

frmMain.Print
frmMain.Print Tab(12); "Single-sided, Double-sided, Single-sided, Double-sided"
frmMain.Print Tab(12); "Low-density  Low-density  High-density  High-density"

For intRow = 1 To 2
    If (intRow = 1) Then
        frmMain.Print "3 1/2 inch"; Tab(15);
    Else
        frmMain.Print "5 1/4 inch"; Tab(15);
    End If
    
    For intCol = 1 To 4
        frmMain.Print curDisks(intRow, intCol); Spc(7);
    Next intCol
    frmMain.Print
Next intRow

End Sub

This procedure produces the output below:

Although the table is small, the 2-by-4 multidimensional array demonstrates how your data sometimes makes a good match for table storage.