30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

248 Arrays Chapter 7<br />

Let us examine array numberArray in Fig. 7.1 more closely. The name of the array<br />

is numberArray. The 12 elements of the array are referred <strong>to</strong> as numberArray(0)<br />

through numberArray(11). The value of numberArray(0) is -45, the value of<br />

numberArray(1) is 6, the value of numberArray(2) is 0, the value of number-<br />

Array(7) is 62 and the value of numberArray(11) is 78. Values s<strong>to</strong>red in arrays can<br />

be employed in various calculations and applications. For example, <strong>to</strong> determine the sum<br />

of the values contained in the first three elements of array numberArray and then s<strong>to</strong>re<br />

the result in variable sum, we would write<br />

sum = numberArray(0) + numberArray(1) + numberArray(2)<br />

To divide the value of the seventh element of array numberArray by 2 and assign the<br />

result <strong>to</strong> the variable result, we would write<br />

result = numberArray(6) \ 2<br />

Common <strong>Program</strong>ming Error 7.1<br />

It is important <strong>to</strong> note the difference between the “seventh element of the array” and “array<br />

element seven.” Array indices begin at 0, which means that the “seventh element of the array”<br />

has the index 6, whereas “array element seven” has the index 7 and is actually the<br />

eighth element of the array. This confusion is a common source of “off-by-one” errors. 7.1<br />

Every array in <strong>Visual</strong> <strong>Basic</strong> “knows” its own length. The length of the array (i.e., 12<br />

in this case) is determined by the following expression:<br />

numberArray.Length<br />

All arrays have access <strong>to</strong> the methods and properties of class System.Array, including<br />

the Length property. For instance, method GetUpperBound returns the index of the<br />

last element in the array. Method GetUpperBound takes one argument indicating a dimension<br />

of the array. We discuss arrays with multiple dimensions in Section 7.9. For onedimensional<br />

arrays, such as numberArray, the argument passed <strong>to</strong> GetUpperBound<br />

is 0. For example, expression<br />

numberArray.GetUpperBound(0)<br />

returns 11. Notice that the value returned by method GetUpperBound is one less than<br />

the value of the array’s Length property. Classes, objects and class methods are discussed<br />

in detail in Chapter 8, Object-Based <strong>Program</strong>ming.<br />

7.3 Declaring and Allocating Arrays<br />

Arrays occupy space in memory. The amount of memory required by an array depends on<br />

the length of the array and the size of the data type of the elements in the array. The declaration<br />

of an array creates a variable that can s<strong>to</strong>re a reference <strong>to</strong> an array but does not create<br />

the array in memory. To declare an array, the programmer provides the array’s name and<br />

data type. The following statement declares the array in Fig. 7.1:<br />

Dim numberArray As Integer()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!