ReDim Statement

This standard VB statement changes the upper and lower bounds of a dynamic array's dimensions.

Syntax

ReDim [Preserve] arrayName (lower To upper) [As [New] type], ...

Argument

Description

arrayName

The name of the array to redimension

lower

The new lower bound for the array

upper

The new upper bound for the array

type

The data type for the array elements

Returns

Not applicable

Usage

ReDim reallocates memory for the dynamic array to support the specified dimensions, and can optionally re-initialize the array elements. ReDim cannot be used at the module level; it must be used inside of a procedure.

The Preserve option is used to change the last dimension in the array while maintaining its contents. If Preserve is not specified, the contents of the array are reinitialized. Numbers are set to zero (0). Strings and variants are set to null ("").

If lower is not specified, 0 is used as the default. The Option Base statement can be used to change the default.

A dynamic array is normally created by using Dim to declare an array without a specified size. The maximum number of dimensions for a dynamic array created in this fashion is 8. If you need more than 8 dimensions, you can use the ReDim statement inside of a procedure to declare an array that has not previously been declared using Dim or Global. In this case, the maximum number of dimensions allowed is 60.

The available data types for arrays are numbers, strings, variants, records, and objects. Arrays of arrays, dialog box records, and objects are not supported.

If the As clause is not used, the type of the variable can be specified by using a type character as a suffix to the name. The two different type-specification methods can be intermixed in a single ReDim statement (although not on the same variable).

The ReDim statement cannot be used to change the number of dimensions of a dynamic array when the array has been given dimensions. It can change only the upper and lower bounds of the dimensions of the array. The LBound and UBound functions can be used to query the current bounds of an array variable's dimensions.

Care should be taken to avoid redimensioning an array in a procedure that has received a reference to an element in the array in an argument; the result is unpredictable.

Example

This example finds the net present value for a series of cash flows. The array variable that holds the cash flow amounts is initially a dynamic array that is redimensioned after the user enters the number of cash flow periods.