Repository navigation
Redim
Home › Array Functions › ReDim
Creates or resizes a variable-length array; elements are reset unless Preserve is used.
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 [, ...]]) [, ...]symbolnameexpression(expression)subscriptlowerbound To ] upperbound
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, so, unlike Dim, variable-length arrays can be defined with constant subscripts.
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.
When used with in a user-defined type, ReDim creates variable-length arrays while being able to pre-size them with constant subscripts.
NOTES:
-
Preservewas in Visual Basic, but not in QBASIC. - Multi-dimensional arrays in FreeBASIC are in row-major order, rather than column-major order.
'' 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)
NextThis program will produce the following output:
index value
0 0
1 1
2 2
3 3
4 4
5 0
6 0
7 0
8 0
9 0'' Define a variable-length array
Dim array() As Integer
'' ReDim array to have 3*4 elements
ReDim array(1 To 3, 1 To 4)
Dim As Integer n = 1, i, j
Print "3 * 4:"
Print
For i = LBound(array, 1) To UBound(array, 1)
For j = LBound(array, 2) To UBound(array, 2)
array(i, j) = n
Print Using "## "; array(i, j);
n += 1
Next
Print
Next
Print
'' ReDim Preserve array to have 4*4 elements, preserving the contents
'' (only the first upper bound should be changed)
ReDim Preserve array(1 To 4, 1 To 4)
Print "4 * 4:"
Print
For i = LBound(array, 1) To UBound(array, 1)
For j = LBound(array, 2) To UBound(array, 2)
Print Using "## "; array(i, j);
Next
Print
Next
Print
'' ReDim Preserve array to have 2*4 elements, preserving but trancating the contents
'' (only the first upper bound should be changed)
ReDim Preserve array(1 To 2, 1 To 4)
Print "2 * 4:"
Print
For i = LBound(array, 1) To UBound(array, 1)
For j = LBound(array, 2) To UBound(array, 2)
Print Using "## "; array(i, j);
Next
Print
Next
PrintThis program will produce the following output:
3 * 4:
1 2 3 4
5 6 7 8
9 10 11 12
4 * 4:
1 2 3 4
5 6 7 8
9 10 11 12
0 0 0 0
2 * 4:
1 2 3 4
5 6 7 8'' 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
'' in order to solve the parsing ambiguity:
'' Redim u(0).array(0 To 9)
'' induces error 4: Duplicated definition, u in 'Redim u(0).array(0 To 9)'
ReDim (u(0).array)(0 To 9)Category: Array Functions
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing