Export (0) Print
Expand All

10.12.1 ReDim Statement

Visual Studio .NET 2003

A ReDim statement instantiates new arrays. Each clause in the statement must be classified as a variable or a property access whose type is an array type or Object, and be followed by a list of array bounds. The number of the bounds must be consistent with the type of the variable; any number of bounds is allowed for Object. At run time, an array is instantiated for each expression from left to right with the specified bounds and then assigned to the variable or property. If the variable type is Object, the number of dimensions is the number of dimensions specified, and the array element type is Object. If the given number of dimensions is incompatible with the variable or property at run time, a System.InvalidCastException will be thrown. For example:

Module Test
    Sub Main()
        Dim o As Object
        Dim b() As Byte
        Dim i(,) As Integer

        ' The next two statements are equivalent.
        ReDim o(10,30)
        o = New Object(10, 30) {}

        ' The next two statements are equivalent.
        ReDim b(10)
        b = New Byte(10) {}

        ' The following statement throws an InvalidCastException.
        ReDim i(10, 30, 40)
    End Sub
End Module

If the Preserve keyword is specified, then the expressions must also be classifiable as a value, and the new size for each dimension except for the rightmost one must be the same as the size of the existing array. The values in the existing array are copied into the new array: if the new array is smaller, the existing values are discarded; if the new array is bigger, the extra elements will be initialized to the default value of the element type of the array. For example, consider the following code:

Imports System

Module Test
    Sub Main()
        Dim x(5, 5) As Integer

        x(3, 3) = 3

        ReDim Preserve x(3, 6)
        Console.WriteLine(x(3, 3) & ", " & x(3, 6))
    End Sub
End Module

It prints the following result:

3, 0

If the existing array reference is null at run time, no error is given. Other than the rightmost dimension, if the size of a dimension changes, a System.ArrayTypeMismatchException will be thrown.

RedimStatement ::= ReDim [ Preserve ] RedimClauses StatementTerminator
RedimClauses ::=
   RedimClause |
   RedimClauses , RedimClause
RedimClause ::= Expression ArraySizeInitializationModifier

See Also

10.12.2 Erase Statement | 10.12 Array-Handling Statements | ReDim Statement (Visual Basic Language Reference)

© 2015 Microsoft