8.2. ArraysAn array is a group of variables (called elements) containing values that all have the same type. Array names follow the same conventions that apply to other variable names. To refer to a particular element in an array, we specify the name of the array and the position number of the element to which we refer. Position numbers are values that indicate specific locations within arrays. Figure 8.1 shows a logical representation of an integer array called c. This array contains 12 elements, any one of which can be referred to by giving the name of the array followed by the position number of the element in parentheses (). The first element in every array is the zeroth element. Thus, the elements of array c are c(0), c(1), c(2) and so on. The highest position number in array c is 11, which is 1 less than 12the number of elements in the array. Figure 8.1. Array consisting of 12 elements.
The position number in parentheses more formally is called an index (or a subscript). An index must be a non-negative integer or integer expression. If a program uses an expression as an index, the expression is evaluated first to determine the index. For example, if variable value1 is equal to 5, and variable value2 is equal to 6, then the statement c(value1 + value2) += 2 adds 2 to array element c(11). Note that an indexed array name (i.e., the array name followed by an index enclosed in parentheses) is an lvalueit can be used on the left side of an assignment statement to place a new value into an array element. Let us examine array c in Fig. 8.1 more closely. The name of the array is c. The 12 elements of the array are referred to as c(0) through c(11)pronounced as "c sub zero" through "c sub 11," where "sub" derives from "subscript." The value of c(0) is -45, the value of c(1) is 6, the value of c(2) is 0, the value of c(7) is 62 and the value of c(11) is 78. Values stored in arrays can be employed in calculations. For example, to determine the total of the values contained in the first three elements of array c and then store the result in variable sum, we would write sum = c(0) + c(1) + c(2) To divide the value of c(6) by 2 and assign the result to the variable result, we would write result = c(6) \ 2 Common Programming Error 8.1
Every array in Visual Basic "knows" its own length. The length of array c (i.e., 12) is determined by the following expression: c.Length All arrays have access to the methods and properties of class System.Array, including the Length property. For instance, method GetUpperBound returns the index of the last element in the array. Method GetUpperBound takes one argument indicating a dimension of the array (e.g., 0 for the first dimension, 1 for the second dimension, etc.). We discuss arrays with multiple dimensions in Section 8.9. For one-dimensional arrays, such as c, the argument passed to GetUpperBound is 0. For example, the expression c.GetUpperBound(0) returns 11. Note that the value returned by method GetUpperBound is one less than the value of the array's Length property. |