Section 8.3. Declaring and Allocating Arrays


8.3. Declaring and Allocating Arrays

Arrays occupy space in memory. The amount of memory required by an array depends on the length of the array and the size of the type of the elements in the array. To declare an array, provide the array's name and type. Either of the following statements can be used to declare the array in Fig. 8.1:

 Dim c As Integer() Dim c() As Integer 


The parentheses that follow the type indicate that c is an array. Arrays can be declared to contain elements of any type; every element of the array is of that type. For example, every element of an Integer array contains an Integer value.

Before an array can be used, you must specify the size of the array and allocate memory for the array, using keyword New. Recall from Chapter 4 that keyword New creates an object. Arrays are objects in Visual Basic, so they too must be allocated using keyword New. The value stored in the array variable is actually a reference to the location in the computer's memory where the array object is stored. All non-primitive and non-structure type variables (such as arrays) are reference variables (normally called references).

The statement

 c = New Integer(11) {} 


allocates memory for the array c after it has been declared. In our example, the number 11 defines the upper bound for the array. Array bounds determine what indices can be used to access an element in the array. Here the array bounds are 0 (which is implicit in the preceding statement and is always the lower bound of every array) and 11an index outside these bounds cannot be used to access elements in the array c. Note that the actual size of the array is one larger (12) than the upper bound specified in the allocation. We also can explicitly specify the array bounds, as in

 c = New Integer(0 To 11) {} 


The explicit array bounds specified in the preceding statement indicate that the lower bound of the array is 0 and the upper bound is 11. Note that the size of the array is still 12.

Common Programming Error 8.2

Explicitly setting the lower bound of an array to a value other than 0 is a compilation error.


The required braces ({ and }) are called an initializer list and specify the initial values of the elements in the array. When the initializer list is empty, the elements in the array are initialized to the default value for the type of the elements of the array. Again, the default value is 0 for numeric primitive data-type variables, False for Boolean variables and Nothing for references. The initializer list also can contain a comma-separated list specifying the initial values of the elements in the array. For instance,

 Dim numbers As Integer() numbers = New Integer() {1, 2, 3, 6} 


declares and allocates an array containing four Integer values. Visual Basic can determine the array bounds from the number of elements in the initializer listit is not necessary to specify the upper bound of the array when a non-empty initializer list is present. When you explicitly state the upper bound and a non-empty initializer list, make sure that the upper bound of the array is one less than the number of elements in the initializer list; otherwise, a compilation error occurs.

The two preceding statements can be combined into a single statement, as in

 Dim numbers As Integer() = New Integer() {1, 2, 3, 6} 




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net