Visual Basic Arrays

The day I “got” arrays is the day I first considered myself a real programmer.  I remember the day well, I was 13 or 14 years old and I remember walking out to meet my friends, really strutting my stuff with a confidence I’d rarely (if ever!!) shown previously.  It had taken a long time to really understand arrays, it was a real painful process, and I couldn’t wait to tell my friends all about it.

I had another painful lesson that day.  Coding does not make you cool.  What you can create with code is cool but the actual act of coding is about as cool as these guys.

Don’t make the same mistake I did.

So why was I so proud?  What is it about Arrays that built and destroyed my confidence in one day?   Like many programming concepts, Arrays have their roots firmly in mathematics.  The legendary mathematician John von Neumann (who is also credited with developing the first computer virus!) invented the first array sorting routine in 1945 and arrays are still firmly entrenched in most, if not all, modern programming languages.

An array is essentially a data structure for storing and retrieving conceptually linked data.  Say, for example, I wanted to store my 8 favourite books on Visual Basic to use in an application.  The books are:

  1. Microsoft Visual Basic 2010 Step By Step
  2. Sam’s Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit
  3. Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175
  4. Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step
  5. Visual Basic 6 Design Patterns
  6. Excel VBA Programming For Dummies
  7. Learn to Program with Visual Basic
  8. Visual Basic 6 Complete

I could use 8 separate variables to store these books.  But what happens if I wanted to add a new one, or remove one?  It would be a real pain to keep adding and removing declarations.  Looping through the variables would be extremely tough.  Rather than using variables, to store this list in Visual Basic I would use an array.

The syntax to declare the array is as follows:

<pre>Dim myArray (7) as String

This tells Visual Basic to reserve in memory a variable called myArray which will store an array of type string.  The number 7 in parenthesis () tells Visual Basic that the Array will have 8 rows.    ”Huh??” I hear you ask, the number 7 tells Visual Basic that the array will have 8 rows?  Yes.  The first row in a Visual Basic array (and arrays in most other languages) is always row 0.   This wasn’t always true in the days of Visual Basic 6.0 but in the world of .NET it is.

Let’s try this for ourselves.  Create a new Windows Forms Project called Arrays.

In the Form Load Event type the following:

<pre>Dim myArray (7) as String

I want to populate the 8 rows of the array with the names of my books.  The syntax to do so is as follows:

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

We essentially read and write to each row using the row index 0 through to 7.  Add this code to your form after the array declaration.

To loop through these items I would probably employ the For Next loop as I know the size of the array.   Either of the Do Loops could be used but, whenever I know upfront how many times to loop, I tend to use the For Loop.  If you remember, it makes the code more readable; I don’t have to read the body of the loop to understand it – it improves the maintainability.

Add a ListBox to your Form called ListBoxBooks.  These book names are quite long so resize the form to look roughly like this:

arrays 1

Add the following code to the Form Load event:

For i As Integer = 0 To myArray.GetUpperBound(0)

This code loops through the array, adding each item to the ListBox.

Run your code and you should see all my Visual Basic books in your ListBox.  Treat them well!

It’s all well and good to know the size of an array when coding but what happens if I decide to buy a new book while the application is running?  The array will need to be resized while the code is executing.  These are called Dynamic Arrays, as opposed to the Fixed Sized Array I declared earlier.

To implement Dynamic Arrays, I would need to resize the array at runtime using the ReDim statement.

Redim myArray(10)

This would resize my array, adding 3 new rows, allowing me to store 11 books.  However this ReDim statement empties the contents of the array after resizing.  To preserve the contents of the array, the Preserve statement would be used.

Redim Preserve myArray(10)

This instructs Visual Basic to extend the array by 3 rows but preserve the contents of the array.

As a rule of thumb, use ReDim as few times as possible as it is a memory hog and performs quite poorly.  The ReDim Preserve statement even more so – the statement actually creates a new array of the new size and copies the contents across.   If the array is large then the statement can take a substantial amount of time to execute, using up a whole load of memory in the process.  Be as efficient as possible with the ReDim statement.  If you absolutely must use it, use it sparingly.  In situations where I don’t know how many items I’ll be storing, or if I know I’ll be resizing the array frequently, I often look at other means of storing my data.

If I don’t know how big my array will be at all, you can instruct Visual Basic to create an empty array using the syntax:

Dim myArray() as String

And later in the code ReDim to the required size.  But remember to be as efficient as possible!

Let’s look again at the the code in your Form.  Look again at the GetUpperBound function.

Back to the code in your Form.  Look again at the GetUpperBound function.


The UpperBound function tells you how many items you have in your array.  What is the 0 for?  It’s to specify the Dimension of the array.  Go to the next tutorial and all will make sense…