Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
136
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

128 Chapter 3 VISUAL BASIC: THE LANGUAGE

Since the index of the first array element is zero, the index of the last element is the length of the array minus 1. Let’s say you have declared an array with the following statement to store player statistics for 15 players, and there are 5 values per player:

Dim Statistics(14, 4) As Integer

The following statements will return the values shown beneath them, in bold:

Console.WriteLine(Statistics.Rank)

2

‘ dimensions in array

Console.WriteLine(Statistics.Length)

75

‘ total elements in array

Console.WriteLine(Statistics.GetLength(0))

15

‘ elements in first dimension

Console.WriteLine(Statistics.GetLength(1))

5

‘ elements in second dimension

Console.WriteLine(Statistics.GetUpperBound(0))

14

‘ last index in the first dimension

Console.WriteLine(Statistics.GetUpperBound(1))

4

‘ last index in the second dimension

Dynamic Arrays

Sometimes you may not know how large to make an array. Instead of making it large enough to hold the (anticipated) maximum number of data (which means that, on the average, most of the array may be empty), you can declare a dynamic array. The size of a dynamic array can vary during the course of the program. Or you might need an array until the user has entered a bunch of data and the application has processed it and displayed the results. Why keep all the data in memory when it is no longer needed? With a dynamic array, you can discard the data and return the resources it occupied to the system.

To create a dynamic array, declare it as usual with the Dim statement (or Public or Private) but don’t specify its dimensions:

Dim DynArray() As Integer

Later in the program, when you know how many elements you want to store in the array, use the ReDim statement to redimension the array, this time to its actual size. In the following example, UserCount is a user-entered value:

ReDim DynArray(UserCount)

The ReDim statement can appear only in a procedure. Unlike the Dim statement, ReDim is executable—it forces the application to carry out an action at runtime. Dim statements aren’t executable, and they can appear outside procedures.

A dynamic array also can be redimensioned to multiple dimensions. Declare it with the Dim statement outside any procedure as follows:

Dim Matrix() As Double

and then use the ReDim statement in a procedure to declare a three-dimensional array:

ReDim Matrix(9, 9, 9)

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

ARRAYS 129

Note that the ReDim statement can’t change the type of the array—that’s why the As clause is missing from the ReDim statement. Moreover, subsequent ReDim statements can change the bounds of the array Matrix but not the number of its dimensions. For example, you can’t use the

statement ReDim Matrix(99, 99) later in your code. Once an array has been redimensioned once, its number of dimensions can’t change. In the preceding example, the Matrix array will remain threedimensional through the course of the application.

Note The ReDim statement can be issued only from within a procedure. In addition, the array to be redimensioned must be visible from within the procedure that calls the ReDim statement.

The Preserve Keyword

Each time you execute the ReDim statement, all the values currently stored in the array are lost. Visual Basic resets the values of the elements as if they were just declared. (It resets numeric elements to zero and String elements to empty strings.)

In most situations, when you resize an array, you no longer care about the data in it. You can, however, change the size of the array without losing its data. The ReDim statement recognizes the Preserve keyword, which forces it to resize the array without discarding the existing data. For example, you can enlarge an array by one element without losing the values of the existing elements by using the UBound() function as follows:

ReDim Preserve DynamicArray(UBound(DynArray) + 1)

If the array DynamicArray held 12 elements, this statement would add one element to the array, the element DynamicArray(12). The values of the elements with indices 0 through 11 wouldn’t change. The UBound() function returns the largest available index (the number of elements) in a one-dimen- sional array. Similarly, the LBound() function returns the smallest index. If an array was declared with the statement

Dim Grades(49) As Integer

then the functions LBound(Grades) and UBound(Grades) would return the values 0 and 49. For more information on the functions LBound() and UBound(), see the reference “VB.NET Functions and Statements” on the CD. Obviously, the LBound() function is of no practical value in VB.NET, since the indexing of all arrays must start at 0.

Arrays of Arrays

Arrays are a major part of the language. In the section “User-Defined Data Types,” earlier in this chapter, you saw how to create arrays of structures. It is possible to create even more complicated structures for storing data, such as arrays of arrays. If an array is declared as Object, you can assign other types to its elements, including arrays.

Note The technique described in this section will work only when the Strict option is Off. If it is On, VB will generate an error to the effect that the Strict option does not allow late binding.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com