Visual Basic Multi-Dimensional Arrays

In the previous tutorial on Arrays we learned that an array is a data structure for storing and retrieving conceptually linked dataWe proceeded to create an array of my Visual Basic books and learnt how to loop through the array.   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.  What happens if I wanted to extend the array to store the author of the books as well?  The array as it stands is essentially a list and is called a one dimensional array.  Adding the author would involve adding another dimension to the array and creating what’s known as a Multi Dimensional Array.  More specifically we’d create a two dimensional array.

Two Dimensional Array

Look at the table below.  The table is made up of a number of rows and two columns.  One of these columns is for storing the Title of the book and another for storing the Author.  This is a classic representation of a two dimensional array.

 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 and 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 title 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 of book```

I have added another dimension to the array.   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.    Exactly like the above table.  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.

To add out the contents of this array to our ListBox we could use a loop.

```For iRow As Integer = 0 To myArray.GetUpperBound(0)
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 of what the 0 represents in this statement:

`myArray.GetUpperBound(0)`

The 0 specifies the Dimension of the array to get the UpperBound of.   As the array is two dimensional we could specify 0 or 1.  If we specified 1 we’d be asking Visual Basic how many columns there are in the array.  By specifying 0 we’re asking how many rows there are.

In the body of the loop 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.  This process of combining strings is called concatenation.  For the first book in my collection the concatenated string will look like:

Microsoft Visual Basic 2010 Step By Step – Michael Halvorson

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

`ReDim myArray (7,2) as String`

This 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 my list of books and the book collections of another 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.

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

This is perhaps one of those topics that is easier to digest by example.  Using Visual Basic I want 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 this 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
• The 1 represents the two columns I am storing for each book (Title and Author)
• The final 1 represents 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 mentioned, a three dimensional array is an arrays of arrays.

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

myArray (row, 0, 1) = Name of Harry’s book
myArray (row, 1, 1) = Author of Harry’s book
```

If we added another dimension and created a 4D array, we’d effectively have an array of array of arrays.  Visual Basic stores up to 32 dimensions but I have never gone 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 one or two dimensional array; you just have another dimension to consider.

For example, to loop through my books we’d use the following code, specifying 0 as the final dimension.

```</span>
<pre>For iRow As Integer = 0 To arrayOfBookCollections.GetUpperBound(0)
ListBoxBooks.Items.Add(arrayOfBookCollections(iRow, 0, 0) & " - " & arrayOfBookCollections(iRow, 1, 0))
Next
```

To loop through Harry’s – note I change the final dimension to access his books.

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

And to loop through both

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

This loop is in fact two nested loops, one to iterate the arrays and one to iterate the rows and columns in those arrays.

That tutorial was a little longer than the previous tutorials but we have covered 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 but you are well on your way to really understanding software development and Visual Basic so well done.