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 modern programming.
An array is essentially a data structure for storing and retrieving linked data. If I have a group of strings that are conceptually linked in some way, I could use an array to store these strings. For example, say I wanted to store a list of my 8 books on Visual Basic.
- Microsoft Visual Basic 2010 Step By Step
- Sams Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit
- Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175
- Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step
- Visual Basic 6 Design Patterns
- Excel VBA Programming For Dummies
- Learn to Program with Visual Basic
- Visual Basic 6 Complete
To store this list in Visual Basic I would use an array.
The syntax to declare the array is as follows:
Dim myArray (7) as String
This tells Visual Basic to reserve in memory an array called myArray. The array will store strings and the 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 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
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 access 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 either know the size of the array. If I don’t know, there are methods available to let me know. The Do Loops could be used but whenever I know upfront how many times to loop, I 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.
Add following code to the Form Load event. It loops through this array, adding each book to the ListBox.
For i As Integer = 0 To myArray.GetUpperBound(0) - 1 ListBoxBooks.Items.Add(myArray(i)) Next
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 at runtime I decide to buy a new book? 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
Would resize my array to have 11 rows, allowing me to store 3 more books. However this statement would empty the contents of the array in the process. To preserve the contents of the array, the Preserve statement would be used.
Redim Preserve myArray(10)
Which 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. Be as efficient as possible with this statement, and if you absolutely must use it, use it sparingly. In situations where I need to resize my array at runtime, I often look at other means of storing my data.
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 with your resizing as it is quite a slow statement to execute.
Back to the code in your Form. Look again at the GetUpperBound function.
What is the 0 for? It’s to specify the Dimension of the array. Go to the next tutorial and all will make sense!