Chapter 11 Using Arrays to Manage Numeric and String Data 295
If you want to focus on array operations without displaying the results, place a comment
character (‘) before each line of code that manipulates a text box object to “comment out”
the text box (but not the progress bar) portions of the program. You’ll be amazed at how fast
array operations run when the results do not need to be displayed on the form. (An array of
100,000 elements loads in just a few seconds .)
Chapter 11 Quick Reference
To Do This
Create an array Dimension the array by using the Dim keyword. For example:
Dim Employees(9) As String
Create a public array Dimension the array by using the Public keyword in a module. For example:
Public Employees(9) As String
Create a public array
specifying upper and
lower bounds
Dimension the array as described earlier, but also use the To keyword. For
example:
Public Employees(0 To 9) As String
Note: The lower bound of the array must always be zero (0).
Assign a value to
an array
Specify the array name, the index of the array element, and the value. For
example:
Employees(5) = "Leslie"
Declare an array
and assign values to
it at the same time
Specify the array name, an array type (optional), and the values for the
array enclosed in braces. For example:
Dim Waiters() As String = {"Ben", "Sue", "Lee", "Kim", "Pat"}
Format text strings
with carriage return
and tab characters
Use the vbCrLf and vbTab constants within your program code. (To add
these values to strings, use the concatenation operator (&) .)
Create a dynamic
array
Specify the name and type of the array, but omit the number of elements.
(If the array has multiple dimensions, insert commas but no numbers
between the dimensions .) In your program code, specify the size of the
array by using the ReDim statement. For example:
ReDim Temperatures(10)
Process the elements
in an array
Write a For... Next loop that uses the loop counter variable to address
each element in the array. For example:
Dim i As Short
Dim Total As Single
For i = 0 To UBound(Temperatures)
Total = Total + Temperatures(i)
Next
Redimension an array
while preserving the
data in it
Use the Preserve keyword in your ReDim statement. For example:
ReDim Preserve myCube(25, 25, 50)