Working with Multi-Dimensional Arrays in VB.Net - September 2011

If you download this tip please consider making a modest donation.

While creating the WonderWord Solver web application I used multi-dimensional arrays to hold variables. Previously I had mostly used single dimension arrays or more recently I've used VB.Net collections. I didn't find much information on the web on working with multi-dimensional arrays. So after some research and lots of trial and error I learned how to use them. I decided to make that recently gained knowledge the September 2011 tip-of-the-month. The knowledge is demonstrated in a web application. I published this application on the web. Click here to run the demo application.

The Demo Application

The Asp.Net, VB.Net, VS/2008 project WorkingWithArrays is documented using screen captures:

Select the first option then click the 'Create It' button.
A randomly sized 2 dimensional array is created.
Click the button again to produce a new array.
Hold the mouse over a cell to display information about the array's entry.

Select the second option then click the 'Create It' button.
A randomly sized 3 dimensional array is created.
Click the button again to produce a new array.

Operative Code

Private Sub Create3DArray()
    Dim sAry3D(1, 1, 1) As String
    Dim iRows As Integer = CInt(Int((miMaxRows * Rnd()) + 1))
    Dim iColumns As Integer = CInt(Int((miMaxColumns * Rnd()) + 1))
    Dim iDepth As Integer = CInt(Int((miMaxDepth * Rnd()) + 1))
    ReDim sAry3D(iRows - 1, iColumns - 1, iDepth - 1)
    For i As Integer = 1 To iRows
        For j As Integer = 1 To iColumns
            For k As Integer = 1 To iDepth
                sAry3D(i - 1, j - 1, k - 1) = i & "." & j & "." & k
    DoDisplayEm3D(Me.TableArray, sAry3D)
End font>

Private Sub DoDisplayEm3D(ByVal oTable As Table, ByVal sAry3D As Array)
    Dim iRows As Integer = UBound(sAry3D, 1) + 1
    Dim iColumns As Integer = UBound(sAry3D, 2) + 1
    Dim iDepth As Integer = UBound(sAry3D, 3) + 1
    For i = 0 To UBound(sAry3D, 1)
        Dim oRow As New TableRow
        For j = 0 To UBound(sAry3D, 2)
            AddRow3D(oRow, sAry3D(i, j, iDepth - 1), i, j, iDepth - 1)
        With oTable
        End With
End Sub

The array is dimensioned in the first highlighted line.
It is redimensioned using randomly generated dimensions in the next highlighted line.
The array is filled with dimension values.
It is passed to the subroutine in the next two highlighted lines.
The highlighted UBound's with corresponding indexes are used to get the size of that dimension.
For example: UBound...3 gets the upper bound of the 3rd dimension.
I found this tip on the web. Click here to view that tip.

Download the VB.Net Code

Download the source code by clicking on the zipped file name. Click here to run the demo application.

Download - Source Zipped File Size
VS/2008 VB.Net, ASP.Net Solution 108K

Previous Tip    Tip Index    Next Tip


Updated 2011/09/02