January 26, 2021
Hot Topics:

Discovering Arrays

  • By Karl Moore
  • Send Email »
  • More Articles »

So far, we've created what is known as a fixed-sized array. That means when we declared the array, we said it would hold five different pieces of information no more, no less.

Of course, in the real world, that's probably a lil' bit impractical.

Let's say you've created the world's greatest sales package. Along with each purchase, the user has to select a sales person from the list.

Now, being the exceptionally brainy developer you are, you've decided to throw all the sales people into an array. No, not literally that could be painful. But you've put their names into an array.

And why not? After all, there are only two-dozen sales chaps and that data is accessed pretty frequently. Instead of constantly dipping into the main database and wasting precious resources, heck, why not just save that information in an array?

However on Monday, you're setting on a new employee, the sexy Swede, Iarma Noying. With the previous fixed-array way of working, you'd have to recompile and redistribute your entire program so your array can hold that one extra person.

But dynamic arrays can change size (dimension) as and when you want them to.

Note: The next few pages work on building a larger project. If you choose to follow the instructions, be sure to label your Command Buttons for clarity otherwise, it'll get mighty confusing. Alternatively, download the project here.

Let's check them out:

  • Create a new project in Visual Basic
  • Insert the following in the General Declarations section behind Form1:
Dim FavouriteThings() As Variant

Hey, you've just declared a dynamic array! It doesn't currently hold anything, but we'll soon change that.

  • Add a Command Button to your Form
  • Change it's Caption property to: Redimension the Array
  • Slot the following code behind your button:
Private Sub Command1_Click()    ReDim FavouriteThings(1 To 5)End Sub

Now this harmless piece of code determines the dimensions of our array one to five. We could have just done it like this:

ReDim FavouriteThings(4)

... but that means we have to use that horrid zero-based counting system. And I don't like that.

This way, we've determined that our FavouriteThings array should hold five different pieces of information, numbered one to five.

Top Tip: If you run ReDim on an array, it clears any existing information within that array unless you include the Preserve keyword, discussed later.

What's so special about redimensioning? Well, you could perhaps find out how many employees you have in your database and then redimension the array appropriately.

So you could, say, dip into your database, open a recordset containing the employees, count how many you have, then 'redimension' the array (ReDim) as we did. After that, you'd simply need to cycle round each record and add it to your array.

But we're not working with databases here, so I'm going to skip all that stuff still, at least you know it's possible. Let's continue.

Top Tip: If you're having problems in working with databases and arrays, post your questions on the Bulletin Board at www.vbforums.com

  • Add another Command Button to your Form
  • Change it's Caption property to: Populate (Fill) the Array
  • Insert the following code behind the button:
Private Sub Command2_Click()    FavouriteThings(1) = "Chocolate"    FavouriteThings(2) = "Summer Breeze"    FavouriteThings(3) = "Lavender"    FavouriteThings(4) = 3    FavouriteThings(5) = 9    End Sub

Here, we're throwing a little information into our array. I'm adding a few of my favourite things from chocolate to the number three.

Another Top Tip: It's worth noting that my FavouriteThings array was declared as a Variant, meaning it can hold many different types of data for example, the string 'Chocolate' as well as the integer 3. However, remember that I-Can-Hold-Anything Variants always take up much more memory than specific Data Types, such as String or Integer so always try to use the type that best suits your needs.

Yet Another Top Tip: One friend of mine needed to analyse statistics from a database. So he threw the numbers into an array and performed all the complex calculations within his Visual Basic application. So it just goes to show that arrays don't limit you to strings you can use them with virtually any data type!

Just One More Jolly Important Top Tip: In addition to declaring your arrays as Strings and Integers, etc you can also use them with custom data types, UDTs. These are particularly helpful if you want to store more than one chunk of information in an array for instance, say you wanted to store both a list of CustomerNames, plus CustomerID numbers in one array. You can do this by (a) defining your own data type (lookup User Defined Types or UDTs in the Visual Basic Help), then (b) declaring your array as that user defined type.

If you like, you can go ahead and test this dynamic array right now. Perhaps you could add a button that displays a particular element. Alternatively, if you're 'au faire' with everything so far, just cross your fingers and continue with the tutorial. It's your call.

Next up, we're going to try and put information into the sixth 'element' of our array. Yes, the sixth non-existent element remember, we only dimensioned our array to hold items one to five. Erm, not six.

As the song goes, 'There May Be Trouble Ahead':

  • Add yet another Command Button to your Form
  • Change it's Caption property to: Put Data into Sixth Element
  • Insert the following code behind the button:
Private Sub Command3_Click()On Error GoTo FavThingsErrFavouriteThings(6) = _InputBox("What do you want to put into the sixth, " & _"non-existent element of the array?", _"Question", "Boxer Dogs")Exit Sub    FavThingsErr:    MsgBox Err.Description    End Sub

This simply takes whatever you enter into the InputBox and attempts to throw it into the sixth element of your FavouriteThings array. But of course, the array doesn't have six elements.

  • Try running the application and clicking on all three command buttons

If you click them out of order, you should receive a few interesting errors. In fact if you click them in order, you should receive a few interesting errors. Coo, life sure is unfair.

Page 5 of 11

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date