Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 11 Using Arrays to Manage Numeric and String Data 275


Tip Arrays that contain a set number of elements are called fixed-size arrays. Arrays that contain
a variable number of elements (arrays that can expand during the execution of the program) are
called dynamic arrays.

Declaring a Fixed-Size Array


The basic syntax for a public fixed-size array is

Dim ArrayName(Dim1Index, Dim2Index, ...) As DataType

The following arguments are important:

n Dim is the keyword that declares the array. Use Public instead if you place the array
in a module.
n ArrayName is the variable name of the array.
n Dim1Index is the upper bound of the first dimension of the array, which is the number
of elements minus 1.
n Dim2Index is the upper bound of the second dimension of the array, which is the
number of elements minus 1. (Additional dimensions can be included if they’re
separated by commas .)
n DataType is a keyword corresponding to the type of data that will be included in the
array.
For example, to declare a one-dimensional string array named Employees that has room
for 10 employee names (numbered 0 through 9), you can type the following in an event
procedure:

Dim Employees(9) As String

In a module, the same array declaration looks like this:

Public Employees(9) As String

You can also explicitly specify the lower bound of the array as zero by using the following
code in an event procedure:

Dim Employees(0 To 9) As String

This “0 to 9” syntax is included to make your code more readable—newcomers to your
program will understand immediately that the Employees array has 10 elements numbered
0 through 9. However, the lower bound of the array must always be zero. You cannot use this
syntax to create a different lower bound for the array.
Free download pdf