Visual Basic Multi-Dimensional Arrays

In this tutorial on Visual Basic Multi-Dimensional Arrays, we will build on what we learned in the last tutorial on arrays and extend our knowledge of arrays into something much more powerful.

In that previous tutorial we learned that an array is a data structure for storing and retrieving conceptually linked data and created an array of my Visual Basic books.   I then showed you how to resize the array using the Redim statement and how the Preserve statement would preserve the contents of the array.

In the previous exercise we used an array to store my Visual Basic Programming books.  What happens if I wanted to store the author as well?  We’d need to extend the array to a two dimensional array.

Two Dimensional Array

Look at the table below.  To store an author in addition to the name of the book, as well as storing information in rows, we’d also have to add a column.

Title Author
Microsoft Visual Basic 2010 Step By Step Michael Halvorson
Sams Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit James Foxal
Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175 Microsoft
Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step G. Andrew Duthie
Visual Basic 6 Design Patterns Microsoft
Excel VBA Programming For Dummies John Walkenbach
Learn to Program with Visual Basic John Smiley
Visual Basic 6 Complete Steve Brown

Let’s look at how to do that programmatically in Visual Basic.  Create a new Windows Forms Project called “2D Arrays”

Add the following declaration to the Form Load event.

Dim myArray (7,1) as String

This declaration tells Visual Basic to create an array of 8 rows and 2 columns (remember Visual Basic counts array rows/columns from 0).

Add the following code to your form to populate the array

myArray (0,0) = “Microsoft Visual Basic 2010 Step By Step”
myArray (0,1) = “Michael Halvorson”
myArray (1,0) = “Sams Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit”
myArray (1,1) = “James Foxal”
myArray (2,0) = “Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175”
myArray (2,1) = “Microsoft”
myArray (3,0) = “Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step”
myArray (3,1) = “G. Andrew Duthie”
myArray (4,0) = “Visual Basic 6 Design Patterns”
myArray (4,1) = “Microsoft”
myArray (5,0) = “Excel VBA Programming For Dummies”
myArray (5,1) = “John Walkenbach”
myArray (6,0) = “Learn to Program with Visual Basic”
myArray (6,1) = “John Smiley”
myArray (7,0) = “Visual Basic 6 Complete”
myArray (7,1) = “Steve Brown”

Look closely at what’s going on here.  In the first column I am storing the name of the book.  In the second column I am storing the name of the author.

myArray (row, 0) = Name of book
myArray (row, 1) = Author book

I have added another dimension to the array.   From one dimensional to two dimensional.  I have created what is known as a multi-dimensional array.  Think of a one dimensional  array as a list.  It’s easiest to visualise a two dimensional array as a grid of rows and columns.    We could have many columns associated with the Name of the book by extending the number of columns in the declaration.

Add a large listbox called ListBoxBooks to your form, much like the one in the last exercise

arrays 1

To add out the contents of this array to our listbox we could use what is known as a nested loop.

For iRow As Integer = 0 To myArray.GetUpperBound(0) - 1
     ListBoxBooks.Items.Add(myArray(iRow, 0) & " - " & myArray(iRow, 1))
Next

You’re familiar with the Loop, however in answer to the last tutorial’s conundrum, the 0 in this line

myArray.GetUpperBound(0)

specifies the Dimension of the array to get the Upperbound of.   As the array is two dimensional we could specify 0 or 1.

In addition, in the body of the loops we have the following code

ListBoxBooks.Items.Add(myArray(iRow, 0) & " - " & myArray(iRow, 1))

Essentially what we have here is a line of code stating:

Add to the ListBox ( Name of Book & ” – ” & ” – ” & Author of Book )

The & (which you may not know is called an ampersand) is joining all the strings together into one long string.  The string in this case for the first book in my collection will look like

Microsoft Visual Basic 2010 Step By Step – Michael Halvorson

We have concatenated a number of strings into one long string using the &.

If I wanted to extend this array and add a new column to store the ISBN of the book I could again use the Redim statement

ReDim myArray (7,2) as String

Which would create an array of 8 rows and 3 columns.

This would of course erase the contents of the array.   If you already have contents in the array and wish to dynamically resize the array at runtime use the preserve keyword

ReDim Preserve myArray (7,2) as String

Remember though, there are performance overheads resizing an array, even more so when preserving the contents, so be as efficient as possible with your Redims!

Three Dimensional Array

So far we have dealt with my list of books.  But what happens if we wanted to store the list of books of 5 people?  What would I do?  You could add another dimension to the array and use what’s known as a three dimensional array.  It’s actually quite hard to visualise a three dimensional array but here’s a great example I found from the University of Liverpool.

Three Dimensional Array

Think of three dimensional array as an array of arrays.   Each “page”, typically the first dimension, contains a two dimensional array.

Lets imagine I wanted to store in an array my collection of Visual Basic books and my friend Harry’s collection of Visual Basic books.

This is my list

Title Author
Microsoft Visual Basic 2010 Step By Step Michael Halvorson
Sams Teach Yourself Visual Basic 2012 in 24 Hours James Foxal
Distributed Applications with Microsoft Visual Basic 6.0 Microsoft

And these is Harry’s

Title Author
Excel VBA Programming For Dummies John Walkenbach
Learn to Program with Visual Basic John Smiley
Visual Basic 6 Complete Steve Brown

To declare a three dimensional array capable of storing these two collections, I would use the following syntax

Dim arrayOfBookCollections (2, 1, 1)

Where the 2 represents the three rows or the three books owned by each of us, 1 represents the two columns or two attributes I am storing for each book (Title and Author) and the final 1 represents the the two collections –  0 being my list and 1 being Harry’s list.

To populate this array I would use the following code

‘My first book
arrayOfBookCollections (0,0,0) = “Microsoft Visual Basic 2010 Step By Step”
arrayOfBookCollections (0,1,0) = “Michael Halvorson”

‘Harry’s first book
arrayOfBookCollections (0,0,1) = “Excel VBA Programming For Dummies”
arrayOfBookCollections (0,1,1) = “John Walkenbach”

‘My second book
arrayOfBookCollections (1,0,0) = “Sams Teach Yourself Visual Basic 2012 in 24 Hours”
arrayOfBookCollections (1,1,0) = “James Foxal”

‘Harry’s second book
arrayOfBookCollections (1,0,1) = “Learn to Program with Visual Basic”
arrayOfBookCollections (1,1,1) = “John Smiley”

‘My third book
arrayOfBookCollections (2,0,0) = “Distributed Applications with Microsoft Visual Basic 6.0”>
arrayOfBookCollections (2,1,0) = “Microsoft”

‘Harry’s third book
arrayOfBookCollections (2,0,1) = “Visual Basic 6 Complete”
arrayOfBookCollections (2,1,1) = “Steve Brown”

As you can see, three dimensional arrays are actually arrays of arrays.    If we added another dimension we’d effectively have an array of array of arrays.  Visual Basic stores up to 32 dimensions but I have never went beyond the third dimension myself.  In the vast majority of cases I only use 1 or 2 dimensions.

Looping through these arrays is exactly the same as a 2D or 1D array, you just have another dimension to consider.

For example, to loop through my books we’d use the following code.

For iRow As Integer = 0 To myArray.GetUpperBound(0) - 1
     ListBoxBooks.Items.Add(myArray(iRow, 0, 0) & " - " & myArray(iRow, 1, 0))
Next

That tutorial was a little longer than I expected but we have leant some extremely powerful concepts here.  I do recommend experimenting with arrays and fully understanding the core concepts of what has been taught here.  This isn’t trivial by any means, you are well on your way to really understanding Visual basic so well done.