ReDim Statement


ReDim Statement

Syntax

     ReDim [Preserve] name(boundList)[, name(boundList)...] 


Preserve (optional)

Preserves existing data within an array when changing the range of the last array dimension.


name (required)

The name of the variable.


boundList (required; numeric)

Comma-delimited list of ranges for each dimension in the array. Each comma-delimited element has the following syntax:

     [0 To] upper 

where upper specifies the new upper bound of the array dimension. The lower bound of each array dimension is always zero. The number of array dimensions must be the same as the dimensions declared in the original Dim statement for the variable.

Description

The ReDim statement is used within a procedure to resize and reallocate storage space for an array. This statement sets new upper bounds in each dimension of the existing array. The original array dimensions for an array variable were defined through the Dim statement or an equivalent statement. Only the range of each dimension can change using the ReDim statement; the number of dimensions cannot be changed. If the Preserve keyword is included, only the last array dimension's range can be adjusted.

Usage at a Glance

  • There is no limit to the number of times you can redimension a dynamic array with the ReDim statement.

  • The number of dimensions cannot be changed, nor can the data type of the array.

  • If you do not use the Preserve keyword, you can resize any or all of the dimensions.

  • You can redimension an array in a called procedure if you pass the array to the procedure by reference. For example:

         Public Sub WorkProcedure(  )        Dim someArray(  ) As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}        Dim oneElement As Integer        ResizeArray(someArray)        ' ----- The following loop will output all 16 elements of        '       the array (0 to 15), including those assigned in        '       the ResizeArray procedure.        For Each oneElement In someArray           Console.WriteLine(oneElement)        Next oneElement     End Sub     Public Sub ResizeArray(ByRef arrayToChange(  ) As Integer)        ReDim Preserve arrayToChange(15)        arrayToChange(10) = 20        arrayToChange(11) = 50        arrayToChange(12) = 80        arrayToChange(13) = 90        arrayToChange(14) = 100        arrayToChange(15) = 200     End Sub 

  • If the ReDim Preserve statement is used to reduce the number of array elements, the data in the discarded elements is lost.

  • Redimensioning an array, particularly a large string array, can be expensive in terms of an application's performance. Frequent redimensioning, as with:

         ReDim Preserve nameSet(UBound(nameSet) + 1) 

    can noticeably degrade your application. You may experience better results if you "pool" the allocation of array elements, redimensioning a block (of say 50 or 100) at once, and not redimensioning again until that block is fully used.

Version Differences

  • VB 6 allowed the initial declaration of an array to use the ReDim statement instead of the Dim statement. With .NET, VB requires separate Dim and ReDim statements for the initial and subsequent allocation actions.

  • In VB 6, only arrays declared without an explicit number of elements, such as:

         Dim vntData(  ) As Variant 

    were dynamic arrays and could be redimensioned using ReDim. In .NET, all arrays are dynamic.

  • VB 6 allowed both the upper and lower bound of each array dimension to change. In .NET, since the lower bound of every dimension is always zero, it cannot be changed.

  • VB 6 permitted changes in the number of array dimensions with the ReDim statement, as long as the Preserve keyword wasn't used. .NET does not support this type of change.

  • Visual Basic 2005 adds an optional lower bound "0 To" clause in each array dimension, for clarity:

         ReDim someArray(0 To 5) 

    Although this appears to specify the lower bound, that lower bound must always be zero.

See Also

Dim Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net