Arrays II


© Swapna Kamat
Articles in this Topic    Discussions in this Topic

Changing the Number of Elements in an Array

Changing the Number of Elements in an Array

Although you usually set the number of elements in an array when you declare it, it's possible to alter the size of the array. When you change the number of elements in an existing array, you redimension it. To do so, use the ReDim keyword in the following syntax:

ReDim [Preserve] ArrayName(Subscript) As DataType

In this syntax,

  • ReDim is the Visual Basic keyword denoting that the array is being redimensioned.
  • Preserve is an optional Visual Basic keyword that forces all pre-existing elements in the array to hold their values. If you don't use the Preserve keyword when you redimension the array, the value of all elements will be changed to zero for numeric data types, and a zero-length string ("") for variable-length strings. Fixed-length strings will be filled with zeros, and variants will be initialized to EMPTY, which could be either zero or a zero-length string, depending on the expression.
  • ArrayName is the name of the array.
  • Subscript is the subscript for the highest element in the array.
  • As is the Visual Basic keyword that signifies a type declaration. When redimensioning an array, the As keyword is optional.
  • DataType is any valid Visual Basic data type, such as Integer or Double. When redimensioning an array, the DataType is optional and can't be changed with the Redim keyword unless the array is of type Variant.
Using ReDim in your code

The actual implementation of the ReDim statement is different from this conceptual illustration. If you create an array that you'll later redimension, you can't hard code the element size of the array when you first declare it.

The following code shows how, conceptually, to redimension the array newArray

ReDim Preserve newArray(9)
newArray(9) = 23

To create an array that you'll later resize, you must first create the array without any elements.

Multidimensional Arrays

So far, we have studied one-dimensional arrays--that is, they are a one-row collection of variables such a row of students. In Visual Basic, however, you can create arrays that have up to 60 dimensions. Usually, two-dimensional arrays will suffice for most introductory programming projects, and most likely you won't need to make an array of more than three dimensions. The higher dimension arrays are required in scientific calculations such as space research and weather forecasting

Think of a two-dimensional array as a tic-tac-toe board--a set of columns and rows that intersect to form a grid. Each grid cell has a location defined by a

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo