Array Usage |
Arrays allow you to refer to a series of variables by the same name and to use a number, called an index or subscript, to tell them apart. This helps you create shorter and simpler code in many situations, because you can set up loops that deal efficiently with any number of elements by using the index number.
An array can have one dimension or more than one. The dimensionality or rank corresponds to the number of subscripts used to identify an individual element. You can specify up to 32 dimensions, although more than three is extremely rare.
Every dimension of an array has a nonzero length. The elements of the array are contiguous along each dimension from subscript 0 through the highest subscript of that dimension. Because Visual Basic allocates space for an array element corresponding to each index number, you should avoid declaring any dimension of an array larger than necessary.
A zero-length array is an array with no elements. It is sometimes necessary to specify such an array, for example when using Windows Forms. To do this, you declare one of the array's dimensions to be �1. The array is then empty, but it still exists. Therefore, a variable pointing to the array is not equal to Nothing.
Arrays do not have fixed size in Visual Basic. You can change the size of an array after you have created it. The ReDim statement assigns a completely new array object to the specified array variable. Therefore, ReDim can change the length of each dimension.
Arrays are objects in Visual Basic .NET, so every array type is an individual reference type. This has the following implications:
All arrays inherit from the Array class in the System namespace, and you can access the methods and properties of System.Array on any array. For example, the Rank property returns the array's rank, and the Sort method sorts its elements.
An array declaration specifies a data type, and all its elements must be of that type. When the data type is Object, the individual elements can contain different kinds of data (objects, strings, numbers, and so on). You can declare an array of any of the fundamental data types, of a structure (described in Structures: Your Own Data Types), or of an object class (described in Object-Oriented Programming in Visual Basic).
You can also declare an array that contains other arrays as its elements. In this case, the constituent arrays must all have the same element data type, which you specify in the declaration. An array of arrays is called a jagged array because the constituent arrays do not have to have the same sizes.
Note Jagged arrays are not compliant with the common language specification (CLS). This means you should not expose jagged arrays from any class you want CLS-compliant code to consume.