FreeBasic Redim Keyword
last modified June 16, 2025
The FreeBasic Redim
keyword is used to resize dynamic arrays
while preserving or clearing their contents. It provides flexibility when
working with arrays whose size isn't known at compile time.
Basic Definition
In FreeBasic, Redim
modifies the dimensions of an existing
dynamic array. The array must first be declared with Dim
or
Redim
without dimensions.
Redim
can change both the lower and upper bounds of an array.
The optional Preserve
keyword maintains existing values when
resizing. Without it, the array contents are cleared.
Basic Redim Usage
This example demonstrates the simplest use of Redim
to create
and resize a dynamic array.
Dim arr() As Integer Redim arr(5) ' Create array with indices 0 to 5 For i As Integer = 0 To 5 arr(i) = i * 10 Next Redim arr(10) ' Resize to indices 0 to 10 For i As Integer = 0 To 10 Print arr(i), Next
First we declare an empty dynamic array. The first Redim
allocates space for 6 elements. After filling it, we resize to 11 elements.
Note that without Preserve
, the original values are lost.
Redim with Preserve
The Preserve
keyword maintains existing values when resizing.
Dim names() As String Redim names(2) names(0) = "Alice" names(1) = "Bob" names(2) = "Charlie" Redim Preserve names(4) names(3) = "David" names(4) = "Eve" For i As Integer = 0 To 4 Print names(i) Next
Here we create a string array with 3 elements. When resizing to 5 elements
with Preserve
, the original names are kept. Only the new
elements need initialization. The array grows while preserving data.
Redim with Custom Bounds
FreeBasic allows specifying both lower and upper bounds when using Redim
.
Dim temperatures() As Single Redim temperatures(1 To 3) temperatures(1) = 22.5 temperatures(2) = 23.1 temperatures(3) = 21.8 Redim Preserve temperatures(1 To 5) For i As Integer = 1 To 5 Print "Day "; i; ": "; temperatures(i) Next
This example creates an array with indices starting at 1 instead of 0.
The first Redim
makes a 3-element array, then we expand it
to 5 elements while keeping existing values. Custom bounds can make code
more intuitive in some cases.
Redim with Multi-dimensional Arrays
Redim
can also work with multi-dimensional arrays.
Dim matrix(,) As Integer Redim matrix(2, 2) ' 3x3 matrix For i As Integer = 0 To 2 For j As Integer = 0 To 2 matrix(i, j) = i * 3 + j Next Next Redim Preserve matrix(2, 4) ' 3x5 matrix For i As Integer = 0 To 2 For j As Integer = 0 To 4 Print matrix(i, j), Next Print Next
We create a 3x3 matrix, fill it with values, then resize to 3x5. Note that
with multi-dimensional arrays, only the last dimension can be changed when
using Preserve
. The other dimensions must remain the same.
Redim with String Arrays
String arrays work similarly with Redim
, with some special
considerations for string memory management.
Dim lines() As String Dim count As Integer = 0 ' Simulate reading lines from a file While count < 5 Redim Preserve lines(count) lines(count) = "Line " & (count + 1) count += 1 Wend For i As Integer = 0 To UBound(lines) Print lines(i) Next
This example shows building a string array incrementally. Each iteration
adds one element to the array. Preserve
maintains existing
strings. String arrays are particularly useful for dynamic content like
file reading or user input.
Redim with User-defined Types
Arrays of user-defined types can also be resized with Redim
.
Type Person name As String age As Integer End Type Dim people() As Person Redim people(2) people(0).name = "Alice" : people(0).age = 25 people(1).name = "Bob" : people(1).age = 30 people(2).name = "Charlie" : people(2).age = 28 Redim Preserve people(4) people(3).name = "David" : people(3).age = 35 people(4).name = "Eve" : people(4).age = 27 For i As Integer = 0 To UBound(people) Print people(i).name, people(i).age Next
Here we create an array of custom Person
types. After initializing
3 elements, we expand to 5 while keeping existing data. Each element is a
structure containing both name and age fields. Preserve
works
the same way with UDTs as with simple types.
Redim with Erase
The Erase
statement can be used with Redim
to
completely clear and resize an array.
Dim values() As Double Redim values(4) For i As Integer = 0 To 4 values(i) = Rnd() * 100 Next Erase values ' Deallocates the array Redim values(9) ' Create new array For i As Integer = 0 To 9 values(i) = Rnd() * 100 Next For i As Integer = 0 To 9 Print values(i), Next
This example shows how Erase
completely clears an array,
freeing its memory. After Erase
, the array must be redimensioned
with Redim
before use. This is useful when you need to start
fresh with a new size.
Best Practices
- Initialization: Always initialize array elements after Redim without Preserve.
- Bounds checking: Use UBound/LBound to avoid out-of-bounds errors.
- Performance: Minimize Redim Preserve operations in loops when possible.
- Memory: Use Erase when done with large arrays to free memory.
- Dimensions: Remember only last dimension can change with multi-dimensional Preserve.
This tutorial covered the FreeBasic Redim
keyword with practical
examples showing dynamic array resizing in various scenarios.
Author
List all FreeBasic Tutorials.