badidea wrote:Bummer, first time I see a use for redim instead of using pointers, turns out it does not work:
Code: Select all
type some_type
as integer array(0, 0)
as integer numX, numY
declare sub init(initNumX as integer, initNumY as integer)
declare sub show()
end type
sub some_type.init(initNumX as integer, initNumY as integer)
dim as integer xi, yi
numX = initNumX
numY = initNumY
redim array(numX, numY)
for yi = 0 to numY-1
for xi = 0 to numX-1
array(xi, yi) = 1
next
next
end sub
sub some_type.show()
dim as integer xi, yi
for yi = 0 to numY-1
for xi = 0 to numX-1
print array(xi, yi);
next
next
end sub
dim as some_type x
x.init(10, 10)
x.show()
sleep
IMHO, in the procedure 'some_type.init', the line 'redim array(numX, numY)' creates a new local array (in the stack):
- You can verify that, replacing 'redim array(numX, numY)' by 'redim
this.array(numX, numY)', and obtaining the compilation error : error 59: Expected array, this in 'redim this.array(numX, numY)'.
- Another way to verify this local array creation (in the stack) is to insert at the beginning of the procedure 'some_type.show' the line 'redim preserve array(numX, numY)' and to observe that all displayed values are equal to 0, despite the keyword "preserve" (new local array initiation in the stack).
Very important in my opinion (as compiling at least once with the option -exx):- Me, in any procedure member, I used to always explicitly address the members names with the prefix:
- 'This.' for non static member name,
- 'Typename.' for static member name,
- '.' for duplicated name (with one member) out of UDT,
in order to avoid conflicts between names.
- With this, such behavior can not happen so, because of detected compilation error.