Visual Basic Arrays

Welcome back to our series of articles about Visual Basic programming. Last article explained the basics of using variables and constants, which were (as we saw) little areas of memory in which we could store values, in order to quickly perform alterations on the values, or just temporarily maintain values, in order to make sure our Form would not be completely filled with TextBoxes that had data in them.

This week, we will be looking at a different type of variables, namely arrays. I know I promised last week to also tackle procedures, but I’ve decided against that, seeing that the discussion of arrays took a little more space than I intended in the first place. Also, I don’t want to create confusion by linking arrays to procedures. I’ll tackle procedures next week. That’s also when we’ll add some functionality to our calculator project.


All right. We saw we use variables to store data in. We could store the name of a book, we could store a date, numbers, etc. What though if we wanted to store two values that are related? For instance, suppose we want to make a list of friend’s birthdays. If we’d do that in Word or HTML, we’d most probably use a table, and would come up with something like this.

Yes, these are their actual birth dates, and yes, programmers get to hang with the cool people 🙂 . Don’t forget to send them a card!

Anyway, as you can see, we’re talking two dimensional data here. It’s not just a name, and it’s not just a date; it’s a combination of both. We could store those values in separate strings and separate data-variables, but we’d end up with a whole lot of them, and it would be rather difficult to see which birth date belongs to which person. Visual Basic luckily offers another way to store two- (or more) dimensional data in: arrays .

Ok, so how would we go about using an array to store the list of friends and their birthdays, as shown above? Simple. We declare an array just the same way as we would declare any variable. The only difference is that we will specify exactly how many dimensions our array will have. In the above example, we have two columns, and five rows. The statement to declare an array for this would be:

Dim strFriends(5, 2) as String

I chose for string here, because of the date. If we’d try and add a date in the format we used (01/01/2002), because of the slash (/), Visual Basic would think we’d want to divide the numbers

The dimensions of an array are quite simple. It might take a little effort to get used to it, but if you’ve dealt with matrices before, this ought to be a rather easy approach. We’ll take the above example again, but this time I’ll show the dimensions next to it.

If we want to set or retrieve data from an array, we always start with the row number, and then use the column number (Same as in Excel. In excel, you deal with cell A1 first). In our example, the name ‘Gary Oldman’ has the ‘coordinates’ (For lack of a better word) 5, 1. Bill Clinton’s birthday’s coordinates are 2, 2.

Let’s put the data from the birthday table into an array in VB. Just start a new EXE-project.

Right under ‘Option Explicit’, we’ll declare the variable:

Dim strFriends(5, 2) As String

Go Back to the Form (double-click it’s name in the Project Explorer), and double-click on the Form. We’ll be taken into the Form_Load event. (This is a From’s basic event, and will be triggered when we actually start a project. As soon as the form becomes loaded, the event fires).

Once we have declared the variable, we can start adding data to it:

strFriends(1, 1) = “Alicia Silverstone”
strFriends(1, 2) = “10/4/1976”

strFriends(2, 1) = “Bill Clinton”
strFriends(2, 2) = “08/19/1946”

strFriends(3, 1) = “Bill Gates”
strFriends(3, 2) = “10/28/1955”

strFriends(4, 1) = “Claudia Schiffer”
strFriends(4, 2) = “08/25/1970”

strFriends(5, 1) = “Gary Oldman”
strFriends(5, 2) = “03/21/1958”

Ok, so now we defined the array, and stored our little friends list in it. But what use is that really, unless we can actually retrieve the values in there? Let’s extend our Form a little. Add the following controls to your Form.

Notice I didn’t name the labels the proper way. Labels are not likely to be addressed in code. I know it’s not conform the standard I brought up a little while ago, but personally, I only use label-naming when I know I’ll change it’s value from code. Also, the TabIndex property might need a little explanation. TabIndex is the order in which you can use the TAB-key to move from one control to another. The control with the lowest TabIndex will first receive focus when the program loads. In other words: In this example, if we start our program, the cursor will be in the txtRow-TextBox. If you’d press TAB, it would move to txtColumns, and lastly to the CommandButton. The last TextBox (txtValue) also has a few new properties. BackColor we saw before. Enabled means that a user can click the Box, and see the cursor move into there. If ‘Enabled’ is ‘False’, this is not possible. ‘Locked’ means a user can’t type data into the TextBox. I combined these three here, to achieve the effect that the boxes’ appearance indicates that it’s only purpose is to provide output, and not input. Lastly, the ‘Default’ property for the CommandButton indicates that this button will activate when the user presses the Enter key. Since this is the only button, we set Default to True. Your form now ought to look similar to this.

See how the bottom TextBox appears to say: “You can’t enter data into me. I’m just meant to present output to you”? Anyway, double-click on the CommandButton, to get us into the Click Event for the button. There, we add the following code:

txtValue.Text = strFriends(Val(txtRow), Val(txtColumn))

If you run the program now, you’ll see that the TabIndex works wonderfully. Also, if you use the Enter key, it’s as if you clicked the button; the Click event will be fired. Enter a number between 1 and 5 in the first box, and 1 or 2 in the second one, and the bottom TextBox will show you the result. Notice that if you use ‘bigger’ coordinates, you will receive an error. That’s because the array is only 5 by 2 for now.

The form of arrays we discussed today is known as ‘Static Arrays’. I.e. we define beforehand exactly how big the array will be. There’s other forms of arrays as well, namely ‘Dynamic Arrays’ (which the dimensions of are not known during the coding), and Control Arrays ( which are arrays of Active-X Controls). We’ll discuss Dynamic Arrays in a future article.

Control Arrays

A Control Array is an array of Active-X Controls. Well, that doesn’t help much, does it? A Control Array exists of a number of the same Controls (For instance buttons) that share the same name, and events. Ah, that’s a lot better, Peter. So what’s the difference between them? Simple. Like the ‘coordinates’ in the array we used, Control Arrays also have dimensions. Controls in a Control Array can be uniquely identified by their ‘Index’-number.

So why would we want to use Control Arrays? Simple. Two reasons:

# Control Arrays are less memory-intensive than the same number of regular controls.
# Control Arrays give you the ability to write one shared Event for all of the controls. This

means you have to write code once in order to respond to the same event for all the buttons (or other control-types) in the Control Array.

In order to create a Control Array, you can do two things:

# Add a control to the Form. Add another control of the same type, and give it the exact same name as the first Control.
# Add a control to your Form. Use Copy (CTRL + C), and then Paste (CTRL + V).

Both of the times you’ll receive a question if you want to create a Control Array. Next week, we’ll make a Control Array out of the buttons on our calculator, and the whole concept might get a bit clearer.