Visual Basic Multidimensional Arrays

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Visual Basic ArraysWorking with Dates and Times in Visual Basic

Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99

Buy eBook

A multidimensional Visual Basic array is nothing more than an array in which each array element is itself an array. A two dimensional array, for example, can be thought of as a table, where each element in the parent array represents a row of the table and the elements of each child array represent the columns of the row. In fact, Visual Basic does not limit an array to two dimensions - up to 32 dimensions are supported.

In this chapter we will cover the creation and use of multidimensional arrays in Visual Basic. To learn how to use single dimensional arrays read Visual Basic Arrays.

Creating a Visual Basic Multidimensional Array

<google>ADSDAQBOX_FLOW</google> Continuing our table analogy, declaring a multidimensional array requires that both the number of rows and columns be defined when the array is created. As with standard arrays, multidimensional arrays are declared using the Dim keyword:

Dim strBooks(4, 2) As String

The above Visual Basic code excerpt creates a two dimensional String array of 5 rows and 2 columns.

Assigning Values to Multidimensional Array Elements

Values are assigned to array elements by specifying the index into each dimension. For example, in a two dimensional array, 0, 0 will references the element in the first column of the first row of the array. The following code initializes each element of our array:

Dim strBooks(4, 1) As String

strBooks (0, 0) = "Learning Visual Basic"
strBooks (0, 1) = "John Smith"
strBooks (1, 0) = "Visual Basic in 1 Week"
strBooks (1, 1) = "Bill White"
strBooks (2, 0) = "Everything about Visual Basic"
strBooks (2, 1) = "Mary Green"
strBooks (3, 0) = "Programming Made Easy"
strBooks (3, 1) = "Mark Wilson"
strBooks (4, 0) = "Visual Basic 101"
strBooks (4, 1) = "Alan Woods"

The above example creates a two dimensional array of 5 rows (remember that indexing starts at 0 so this array have elements from 0 to 4 inclusive) and two columns. Column 1 is the book title and column 2 the author.

Accessing Elements of a Multidimensional Array

Array elements are accessed using the same technique as that used to assign values to elements, by specifying the index into each dimension. The following example uses a nested For loop to iterate through a two dimensional array and display each element in a MessageBox:

       For intCount1 = 0 To 4
            For intCount2 = 0 To 1
                MessageBox.Show(strBooks(intCount1, intCount2))
            Next intCount2
        Next intCount1

A similar approach can be used to iterate through the element of a three dimensional Visual Basic Array:

    For intCount1 = 0 To 5
       For intCount2 = 0 To 4
            For intCount3 = 0 To 1
                MessageBox.Show(strBooks(intCount1, intCount2, intCount3))
            Next intCount3
        Next intCount2
    Next intCount1


This chapter has covered the basic concepts of working with arrays in Visual Basic.

Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99

Buy eBook