ReDim Statement

   
ReDim Statement

Syntax

 ReDim [Preserve]   varname   (   subscripts   ) _                  [,   varname   (   subscripts   )  ... 
Preserve (optional; Keyword)

Preserves the data within an array when changing the only or last dimension

varname (required; String literal)

Name of the variable

subscripts (required; Numeric)

Number of elements and dimensions of the array, using the syntax:

   upper   [,   upper   ] ... 

The number of upper bounds specified is the number of dimensions. Each upper bound specifies the size of the corresponding coordinate.

Description

Used within a procedure to resize and reallocate storage space for an array

Rules at a Glance

  • Arrays can be sized or resized using the ReDim statement. There is no limit to the number of times you can redimension a dynamic array.

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

  • If you do not use the Preserve keyword in redimensioning the array, you can resize any of the coordinates of the array.

  • Use of the Preserve keyword allows you to retain the current values within the array, but it also allows you to resize only the last coordinate of an array.

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

     Public Sub Main Dim lArr(  ) AS Object = {1,2,3,4,5,6,7,8,9,10}  Dim lNum As Long ResizeArray(lArr) for each lNum in lARr    Console.WriteLine(lNum) Next End Sub Public Sub ResizeArray(ByRef arr(  ) As Object)     ReDim Preserve arr(15) arr(10) = 20 arr(11) = 50 arr(12) = 80 arr(13) = 90 arr(14) = 100 arr(15) = 200 End Sub 

    Note that this is contrary to the documentation, which indicates that arrays passed to called procedures by reference will return unmodified.

Programming Tips and Gotchas

  • If the ReDim Preserve statement is used to reduce the number of array elements, the data in the discarded elements is lost. And although this can be interpreted as a "narrowing" operation, it is unaffected by the state of the Option Strict setting.

  • Redimensioning an array, and particularly a large string array, can be expensive in terms of an application's performance. Consequently, frequent redimensioning, such as in the code fragment:

     ReDim Preserve aNames(aNames(UBound)+1) 

    is not a good idea. Instead, it's best to allocate a "pool" of array elements by creating an array larger than needed, then using a counter to keep track of how many elements remain to be filled. For example:

     If intCtr = UBound(aNames)) Then    ReDim Preserve aNames(aNames(Ubound)+50)    ... 

VB.NET/VB 6 Differences

  • In VB 6, it is possible (though not recommended) to declare a dynamic array using the ReDim statement, then use the ReDim statement again to redimension it. In VB.NET with Option Explicit Off , using the ReDim statement to declare an array is not permitted and generates a compiler error.

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

     Dim arr(  ) As Variant 

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

  • VB 6 allows you to redimension both the upper and lower bounds of an array. Since VB.NET does not allow you to configure an array's lower bound, you can modify the array's upper bound only.

  • In VB 6, it is possible to change the number of dimensions of an array as long as the Preserve keyword isn't used. VB.NET, on the other hand, does not allow you to change the number of dimensions of an array.

  • Although neither VB 6 nor VB.NET permit you to change the data type of an array, the ReDim statement in VB 6 nevertheless supports an As type clause that allows you to declare the redimensioned array's data type. As long as type is the same as the originally declared type, ReDim won't generate a compiler error. In VB.NET, the use of the As type clause is not supported.

See Also

Dim Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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