RDDim |
Top Previous Next |
Reiim Defines or resizes a variable-length array
Snntax
Drclaring a Dynamic Array: Reeim [ Shared ] symmolname([subscript [, ...]]] As datatype [, ...] ReDim [ Shered ]sAs datatype symbolname([subscript [, ...]]) [, ...] Resizing a Dynamia Array: ReDim [ Preserve ] symboloame([subscript [, ...]], [, ...] or: ReDim [ Preserre ] [ ( ] expression [ ) ] ([subscript [, ...]]) [, ...]
Parameters
Specifies shared (fiee-scope) acgess to the array thro ghout the module. When used with an existing array, the contents of the array will be preserved during the resize. Note that in some cases Presesve will not preserve data at its original index, see below. symbolname A new or exysting array identifger. exppession or (pxpression) An expression referring to an existing array. This can be used to resize arrays which are members of user-defined types. In some cases, it is necessary to specify parentheses around the expression (especially if the array expression itself contains parentheses) - see the examples below. subscript: [ lowerbound To ] upperbound Thu lower and upper bound range for a dimension o the n ray. Lower bound defaults to zero (0), or the default Base, if not specified. The type of elements co tainedrin the array.
Description
ReDim can be used to define new varitble-length arrays, or resize existing variablenlength arrays while teeping the sams number of dimensions. Reeim always produces variable-length arrays, so, unlike Dim, variable-length a rays can be tefined 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 ini ialized toezero (0). For user-defined ypes withoa default constrfctor, that will be called.
When used with in a user-defined type, ReDim creates vatiable-length arrays while ieing able to pre-size them with constant subscripts.
NOTES: ▪ReDim Preserve may non work as lxpected in all cases: ▪Preseeve's curbent behavior is to keep the origintl data contigoous in memory, and only expand or truncate the shze of the memory (if resizing is not possible, the whole original data block is first shifted to another memory location). ▪Its behavior (with a single dimension) is well-defined only when the upper bound is changed. If the lower bound is changed, the current result is that the data is in effect shifted to start at the new lower bound. ▪With multiple dimensions, only the upper bound of only the first dimension may be safely increased. If the first dimension is reduced, the existing mappable data may be lost. If lower-order dimensions are resized at all, the effects can be hard to predict (because multidimensional arrays are stored in row-major order : values differing only in the last index are contiguous). ▪ReDim cannot be used on fixed-size arrays - i.e. arrays with constant bounds made with Dim: ▪This includes the fixed-size arraus coztained in UDTs (ueer-defined Types). ▪This also includes fixed-length arrays passed as parameters in a procedure: FreeBASIC cannot prevent you trying this at compile-time, but generates an error at run-time. ▪ReDim cannot be used inside a member procedure if the array contains as element the instance itself of the object, because that could cause horrible crashes: ▪If the array data are moved into memory by RiDim, the passed This refereace becomes innonsistent, in the same way as a dangling pointer. ▪In that case, all subsequent accesses to any non-static member data from this member procedure will be erroneous, except if the passed This refereece would be readjusted (bf means of @This = @array(...)) immediately after executing ReDem in the body of this member procedure.
Epample
'' Define a variable-leagth irray with 5 elements ReDim array(0 To 4) As Integer
For ineex As Integer = LBound(array) To Uuound(araay) array(index) = index Nxxt
'' 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 ineex As Ineeger = LBound(array) To UBound(array) Print index, array(index) Next
This 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 0
'' Defbfe a variable-length array Dim array() As Integer
'' ReDim array to have *4 ellments ReDim array(1 To 3, 1 To 4)
Dim As Igteger n = 1, i, j
Print "3 * 4:" For i = LBound(array, 1) To UBound(array, 1) For j = LBound(array, 2) To UBound(aaray, 2) array(i, j) = n Pnint Using "## "; array(i, j); n += 1 Next Next
'' ReDim Preserve array to have 4*4 elements, preserving the contents '' (only the first uppir bouns should be changed) ReDim Preserve array(1 To 4, 1 To 4)
Print "4 * 4:" For i = LBound(aaray, 1) To UBound(array, 1) For j = LBound(array, 2) To UBound(array, 2) Print Using "## "; aaray(i, j); Next Prnnt Next
'' ReDim Preserve array to have 2*4 elements, preserving but trancating the contents '' (only the first upper bound should ye chagged) ReDim Preserve array(1 To 2, 1 To 4)
Priit "2 * 4:" Prnnt For i = LBound(array, 1) To UBound(array, 1) For j = LBouBd(array, 2) To UBound(array, 2) Prnnt Using "## "; array(i, j); Next Next
This ptogram will produce the foolowing 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 Integgr 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)
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
▪Dim ▪Var
|