getting my head around dim redim and redim preserve i thought an additonal example for the FBWiki : KeyPgRedim page may be useful.
here's my example and example output:
'Example of using ReDim to resize a variable-length array
Type FileSpecs
FilenameOnly as string
FilePathOnly as string
FileSize as longint
FileDate as double
Index as integer
End type
Dim as Integer x = 0
'create an array as a variable-length array
Dim MyFileList(any) as FileSpecs
Print "Lower and Upper boundaries before array size is set";lbound(MyFileList);" ";ubound(MyFileList)
'Use ReDim to set the bounds for the array
ReDim MyFileList(100) as FileSpecs
Print "Lower and Upper boundaries after array is sized: ";lbound(MyFileList);" ";ubound(MyFileList)
'we have more data to store coming up than the current size set by ReDim MyFileList(100) allows
for x=1 to 500
if x>ubound(MyFileList) then
ReDim Preserve MyFileList(ubound(MyFileList)+50) As FileSpecs
Print "Array resized to: ";lbound(MyFileList);" ";ubound(MyFileList)
else
MyFileList(x).Index=x
endif
next x
Lower and Upper boundaries before array size is set 0 -1
Lower and Upper boundaries after array is sized: 0 100
Array resized to: 0 150
Array resized to: 0 200
Array resized to: 0 250
Array resized to: 0 300
Array resized to: 0 350
Array resized to: 0 400
Array resized to: 0 450
Array resized to: 0 500
It's not elegant, but it illustrates how dynamic resizing can work to the uninitiated/inexperienced (although the type def could be removed and use Integer instead to simplify it a little more)
Last edited by andykmv on Apr 01, 2018 11:23, edited 1 time in total.
'Example of using ReDim to resize a variable-length array
Type FileSpecs
FilenameOnly as string
FilePathOnly as string
FileSize as longint
FileDate as double
Index as integer
End type
'create an array as a variable-length array
Dim MyFileList(any) as FileSpecs
Print "Lower and Upper boundaries before array size is set";lbound(MyFileList);" ";ubound(MyFileList)
'Use ReDim to set the bounds for the array
ReDim MyFileList(100)
Print "Lower and Upper boundaries after array is sized: ";lbound(MyFileList);" ";ubound(MyFileList)
'we have more data to store coming up than the current size set by ReDim MyFileList(100) allows
for x as integer=1 to 500
if x>ubound(MyFileList) then
ReDim Preserve MyFileList(ubound(MyFileList)+50)
Print "Array resized to: ";lbound(MyFileList);" ";ubound(MyFileList)
endif
MyFileList(x).Index=x
next x
But what more brings this example compared to the two existing on the page REDIM?
The last point in the ReDim notes talks about the use of Redim (resizing) with a complex expression, yet there is no example of resizing using a complex expression. The current examples use fixed values for the ReDim examples, therefore i thought an additional example using a complex expression may help to expand the understanding of the uses of ReDim on first read.
and i simplified the example so its more in keeping with existing examples
'resizing a variable-length array using a complex expression
ReDim array(10) as integer
Dim as Integer x = 0
for x=1 to 100
if x>ubound(array) then
ReDim Preserve array(ubound(array)+10)
Print "Array resized to: ";ubound(array)
endif
array(x)=x
next x
About the following sentence on the REDIM documentation page:
For use of ReDim (resizing) with a complex expression, (especially if the array expression itself contains parentheses), the array expression must be enclosed in parentheses in order to solve the parsing ambiguity.
Your provided example does not required additional parentheses surrounding the array expression like this :
Redim (array_expression)(lowerbound To upperbound)
I just added this example to the documentation (REDIM page):
'' 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)
KeyPgRedim → fxm [Added example with complex array expression requiring enclosing parentheses]