Skip to content

REDIM

Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgRedim Last revised: 2024-06-19

Declares or resizes a dynamic (variable-length) array.

Syntax

Declaring a Dynamic Array:

Redim [Shared] symbolname( subscript [, ...] ) As datatype [, ...]
Redim [Shared] As datatype symbolname( subscript [, ...] ) [, ...]

Resizing a Dynamic Array:

Redim [Preserve] symbolname( subscript [, ...] ) [, ...]

or:

Redim [Preserve] [ ( ] expression [ ) ] ( subscript [, ...] ) [, ...]

Parameters

  • Shared — Specifies shared (file-scope) access to the array throughout the module.
  • Preserve — When used with an existing array, the contents of the array will be preserved during the resize.
  • symbolname — A new or existing array identifier.
  • expression — An expression referring to an existing array (used to resize arrays which are members of UDTs).
  • subscript: [lowerbound To] upperbound — The lower and upper bound range. Lower bound defaults to zero (0) if not specified.
  • datatype — The type of elements contained in the array.

Description

Redim can be used to define new variable-length arrays, or resize existing variable-length arrays while keeping the same number of dimensions. Redim always produces variable-length arrays.

When defining a new variable-length array, its elements are default constructed. For simple data types like Integer or Double, the elements are initialized to zero (0). For user-defined types with a default constructor, that will be called.

Notes on Redim Preserve

  • Preserve's current behavior is to keep the original data contiguous in memory, and only expand or truncate the size of the memory.
  • Its behavior (with a single dimension) is well-defined only when the upper bound is changed.
  • With multiple dimensions, only the upper bound of only the first dimension may be safely increased.

Restrictions

  • Redim cannot be used on fixed-size arrays (arrays with constant bounds made with Dim).
  • Redim cannot be used inside a member procedure if the array contains as element the instance itself of the object.

Examples

vb
' Define a variable-length array with 5 elements
ReDim array(0 To 4) As Integer
For index As Integer = LBound(array) To UBound(array)
    array(index) = index
Next

' Resize a variable-length array with 10 elements
' (the lower bound should be kept the same)
ReDim Preserve array(0 To 9)
Print "index", "value"
For index As Integer = LBound(array) To UBound(array)
    Print index, array(index)
Next

Output:

index         value
 0             0
 1             1
 2             2
 3             3
 4             4
 5             0
 6             0
 7             0
 8             0
 9             0
vb
' Define a variable-length array as UDT field
Type UDT
    Dim As Integer array(Any)
End Type
Dim As UDT u(0 To 3)

' For use of Redim with a complex array expression
' (especially if the array expression itself contains parentheses),
' the array expression must be enclosed in parentheses
ReDim (u(0).array)(0 To 9)

Differences from QB

  • Preserve was in Visual Basic, but not in QBASIC.
  • Multi-dimensional arrays in FreeBASIC are in row-major order, rather than column-major order.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft