Dynamic Arrays

Dynamic Arrays

In the previous lesson, all of our variable arrays were of a fixed size. For example, an array was dimensioned with indexes from 1 to 5, and did not allow anything beyond the upper bound. A dynamic array is a special class of array whose upper bound can be changed at run-time. This means that the array can grow as necessary. Dynamic arrays are ideal when the number of items to be stored is not known when the program begins.

To accomplish a dynamic array, the variable is dimensioned like a regular array but no bounds are specified. For example,

Dim strNames() As String

will dimension a dynamic string array called strNames. Its lower and upper bound have not yet been set.

To set and change the boundaries, the ReDim statement is used later in the code:

ReDim strNames(1 to 12) as String

This allows the size to be changeable; the upper bound 12 could be specified with a variable like intNameCount. The following example demonstrates how this can be used.

Example 1
' dimension
Dim strNames() As String
Dim intTotal as Integer
Dim intCount as Integer
' initialize
intTotal = InputBox("Please enter the number of names allowed:")
' resize array
ReDim strNames(1 to intTotal)
' get names
For intCount = 1 to intTotal
    strNames(intCount) = InputBox("Enter name " & CStr(intCount) & ":")
Next intCount

In Example 1, what messages are displayed in the input boxes? What is the advantage of using a dynamic array?

Note: Static arrays (those that are not dynamic) are slightly more efficient than dynamic arrays, meaning that the computer must work a little harder to access and redimension a dynamic array than a static array. When possible, you should use static arrays. Dynamic arrays are reserved for cases in which the number of elements needed in an array is not known.

ReDim Preserve

Visual Basic offers one more useful option in dealing with dynamic arrays. You can even change the upper bound of an array while it has data stored in it. This means that if five names have already been entered, and the upper bound has been reached, the array can be redimensioned to allow more names.

For example, the code:

ReDim Preserve strNames(1 to 6)

would change the size of the strNames array so that it contained 6 elements, without destroying the information already stored in the array.

Linear Search

One important need in programming is the ability to search a list of data for a specific item. This is done most simply with a linear search, in which the program starts at the top of the list and compares each item until it finds a match or reaches the end of the list.

A linear search is best accomplished with a Do...Loop because the type of loop will allow the search to terminate when a match is found, whereas a For...Next Loop will search the entire list even if a match is found near the beginning.

A simple example of a linear search is demonstrated by the following algorithm:

Algorithm 1: Linear Search
intSearchPos = 1 so we start at the beginning
Do Until intSearchPos > Length Of List Or blnFound
    If ArrayVariable(intSearchPos) = the string we're looking for Then
        Display "found" message at position intSearchPos
        blnFound = True so the loop will end
    End If
    Increment search position

Class Exercise: Class List

If time permits, begin work on the class exercise in Lesson 3.