You are here:
Basic > VB6
<< Previous |
Contents | Next >>
Arrays of arrays in VB6 (Visual Basic 6)
While you can create two-dimensional arrays in Visual Basic, their structure
isn't really flexible for at least two reasons: All rows in the array must have
the same number of elements, and you can use ReDim Preserve to change the number
of columns but you can't add new rows. The first point is especially important
because it often leads you to declare an array that's far too large for your needs,
thus allocating a lot of memory that in most cases remains largely unused. You
can solve both problems using a structure known as an array of arrays.
The technique is conceptually simple: Since you can store an array in a Variant
variable, you can build an array of Variants, where each item holds an array.
Each subarray—a row of this pseudo-array—can hold a different number
of elements, and you don't need to use more memory than is strictly necessary.
Here's an example, based on an imaginary PIM (Personal Information Manager)
program. In this program, you need to keep track of a list of appointments for
each day of the year. The simplest solution would be to use an array in which
each row corresponds to a day in the year and each column to a possible appointment.
(For the sake of simplicity, let's assume that each appointment's data can be
held in a string.)
ReDim apps(1 To 366, 1 To MAX_APPOINTMENTS) As String
Of course, you now have the problem of setting a reasonable value for the
MAX_APPOINTMENTS symbolic constant. It should be high enough to account for all
possible appointments in a day but not too high because you might be wasting a
lot of memory without any real reason. Let's see how the array of arrays technique
can help us save memory without posing any artificial limit to your application:
' A module-level variable
Dim apps(1 To 366) As Variant
' Add an appointment for a given day.
Sub AddNewAppointment(day As Integer, description As String)
Dim arr As Variant
If IsEmpty(apps(day)) Then
' This is the first appointment for this day.
apps(day) = Array(description)
' Add the appointment to those already scheduled.
arr = apps(day)
ReDim Preserve arr(0 To UBound(arr) + 1) As Variant
arr(UBound(arr)) = description
apps(day) = arr
' Extract all the appointments for a given day.
In this example, I kept the code as simple as possible and used an array of Variant
arrays. You could save even more memory if each row of this array were built using
an array of a more specific data type (String, in this case). Note the special
syntax used to address an item in an array of arrays:
Sub ListAppointments(day As Integer, lst As ListBox)
Dim i As Long
For i = 0 To UBound(apps(1))
' Change the description
for the Nth appointment.
apps(day)(n) = newDescription
Nothing keeps you from extending this concept further, introducing an array
of arrays of arrays, and so on. If you're dealing with arrays in which each row
can vary considerably in length, this approach is going to save you a lot of memory
and, in most cases, improve your overall performance too. A key feature of an
array of arrays is that you can process entire rows of your pseudo-array as if
they were single entities. For example, you can swap them, replace them, add and
delete them, and so on.
' Move the January 1st appointments to January 2nd.
apps(2) = apps(1)
apps(1) = Empty
Finally, an important advantage of this technique is that you can add new rows
without losing the current contents of the array. (Remember that you can use ReDim
Preserve on regular arrays only to modify the number of columns, not the number
' Extend the appointment book for another nonleap year.
ReDim Preserve apps(1 to UBound(apps) + 365) As Variant
More Topics on Visual Basic 6 Arrays
<< Previous | Contents
| Next >>