What Is an Array?
A collection of similar variables, in which each has the same name and all are of the same type, is an array. Remember that a variable can be thought of as a cup that holds an unknown value or an always changing value
Think of an array, then, as a collection of cups. Each cup in the collection can hold the same type of data, and every cup in the collection has the same name. Each cup within the collection is an element and has a number assigned to it that reflects its position within the collection. The first element of an array usually has a position number of 0 (zero).
Arrays can be different sizes. One array might have three elements, another might have 30 elements, and it's even possible for an array to have no elements at all--only the possibility of having elements that are created at a later time.
Declaring Arrays
You can declare, or dimension, an array in two ways: as you would a single variable and by using the To keyword.
Declaring an Array like Declaring a Single Variable
To declare an array as you would a single variable, you use the following syntax:
Dim|Public|Private ArrayName(Subscript) As DataType
In this syntax, Dim, Public, and Private are Visual Basic keywords that declare the array and its scope. If you use Dim, the array is private to the procedure in which it is declared. Public makes the array visible from anywhere in the program, and Private (within the General section of a form or module) makes the array visible only to the form or module in which it's declared. If you use Dim within a module's procedure, the array will be available to only that procedure, but if you use Dim in the module's Declarations section, the array will be available to all procedures within the module.
ArrayName is the name of the array.
Option base
When you declare an array, the first element of the array is usually 0 (zero). It's possible, however, to force the first element of an array to be 1. To do this, insert the statement Option Base 1 in the General section of a module within your project. You have to do this only when you want the first element of your array to be element number 1.
Subscript is the number of the highest element in the array. Remember that the first element in an array is usually zero, so if you declare an array in which Subscript is 6, you'll have seven element positions and, therefore, seven elements.
As is the Visual Basic keyword that signifies a type declaration.
DataType is any valid Visual Basic data type, such as Integer or Double.
Therefore, to declare an array of integers with five elements in it, you would use the following:
Dim iMyArray(4) As Integer
To assign a value to each element in the array iMyArray, you would use the following:
iMyArray(0) = 9
iMyArray(1) = 342
iMyArray(2) = 2746
iMyArray(3) = 0
iMyArray(4) = 8901
To change the value of the fourth element of the array iMyArray from 0 (zero) to 45, you would do as follows:
iMyArray(3) = 45
To declare an array of nine strings global to your entire program, you would use the code in Listing 3.10.
LISTING 3.10 LIST01.TXT--Assigning Values to an Array's Elements
01 Public strMyArray(8) As String
02
03 strMyArray(0) = "I am a pitcher."
04 strMyArray(1) = "I am a catcher."
05 strMyArray(2) = "I play first base."
06 strMyArray(3) = "I play second base."
07 strMyArray(4) = "I play third base."
08 strMyArray(5) = "I play shortstop."
09 strMyArray(6) = "I play left field."
10 strMyArray(7) = "I play center field."
11 strMyArray(8) = "I play right field."
Declaring an Array with the To Keyword
You can also declare an array by using the To keyword within the subscript syntax. For example, if you want to create an array of five Integer variables in which the first element is number 1 and the last element is number 5, you use
Dim iMyArray(1 To 5) as Integer
This method provides an easy way to start your element numbering at a value other than 0 (zero).
Dynamic arrays
Although you usually set the number of elements in an array when you declare it, it's possible to alter the size of the array. When you change the number of elements in an existing array, you redimension it.
To do so, use the ReDim keyword in the following syntax:
ReDim [Preserve] ArrayName(Subscript) As DataType
In this syntax,
ReDim is the Visual Basic keyword denoting that the array is being redimensioned.
Preserve is an optional Visual Basic keyword that forces all pre-existing elements in the array to hold their values. If you don't use the Preserve keyword when you redimension the array, the value of all elements will be changed to zero for numeric data types, and a zero-length string ("") for variable-length strings. Fixed-length strings will be filled with zeros, and variants will be initialized to EMPTY, which could be either zero or a zero-length string, depending on the expression.
ArrayName is the name of the array.
Subscript is the subscript for the highest element in the array.
As is the Visual Basic keyword that signifies a type declaration. When redimensioning an array, the As keyword is optional.
DataType is any valid Visual Basic data type, such as Integer or Double. When redimensioning an array, the DataType is optional and can't be changed with the Redim keyword unless the array is of type Variant.
Using ReDim in your code
The actual implementation of the ReDim statement is different from this conceptual illustration. If you create an array that you'll later redimension, you can't hard code the element size of the array when you first declare it. Therefore, in practice, the code here won't re dimension the array declared in Listing 3.10
The following code shows how, conceptually, to redimension the array strMyArray (from Listing 3.10) to contain 10 elements:
ReDim Preserve strMyArray(9)
strMyArray(9) = "I am the designated hitter."
To create an array that you'll later resize, you must first create the array without any elements. Listing 3.11 shows the proper way to create an array that you'll later redimension.
LISTING 3.11 LIST02.TXT--Diming an Array Without Any Elements Before
ReDiming It
01 `Create an array without any elements
02 Dim strMyArray() as String
03
04 `Dimension the array for 9 elements
05 ReDim strMyArray(8)
06
07 `Assign values to the array elements
08 strMyArray(0) = "I am a pitcher."
09 strMyArray(1) = "I am a catcher."
10 strMyArray(2) = "I play first base."
11 strMyArray(3) = "I play second base."
12 strMyArray(4) = "I play third base."
13 strMyArray(5) = "I play shortstop."
14 strMyArray(6) = "I play left field."
15 strMyArray(7) = "I play center field."
16 strMyArray(8) = "I play right field."
17
18 `Add an element and make it so all the values
19 `of the previous elements are kept intact
20 ReDim Preserve strMyArray(9)
21
22 `Assign a value to the new array element
23 strMyArray(9) = "I am the designated hitter."
Notice in Listing 3.11 that the first ReDim statement (line 5) doesn't use the Preserve keyword. You can do this because initially there are no values within the array to preserve. In the second ReDim statement on line 20, however, the Preserve keyword is very important because the pre-existing elements have assigned values that you don't want to lose. If you didn't use the Preserve keyword in the second ReDim statement, strMyArray would have the following values:
strMyArray (0) = ""
strMyArray (1) = ""
strMyArray (2) = ""
strMyArray (3) = ""
strMyArray (4) = ""
strMyArray (5) = ""
strMyArray (6) = ""
strMyArray (7) = ""
strMyArray (8) = ""
strMyArray (9) = "I am the designated hitter."
Remember, arrays are tricky. Be careful!
Saturday, December 26, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment