02-03-2024, 04:29 PM
(02-03-2024, 02:48 PM)bartok Wrote: I dony' understand. Look at this code:In this example a dynamic array is created ( b%() ) and then immediately removed with CLEAR. However, inside the loop the dynamic array is once again created when i% equals 1 and then increased in size when i% is greater than 1. See below. Your code actually creates b%() twice, not once to be reused.
Code: (Select All)
'DIM SHARED b%(30) 'if activated, the program doesn't work.
REDIM SHARED b%(30) 'if activated, the program works.
'if both deactivated, the program doesn't work.
CLEAR
FOR i% = 1 TO 40
REDIM _PRESERVE b%(i%)
b%(i%) = i%
NEXT i%
CALL ciao
SUB ciao
PRINT b%(3)
END SUB
So, if CLEAR completely remove b%(), so that it doesn't exist anymore, why it is however needed REDIM SHARED b%(30) before CLEAR? It seems that CLEAR completely remove b%(), without removing the information that it is SHARED. But how is it possible that an inexistent array could be SHARED?
Code: (Select All)
' here the code is creating a dynamic array of 31 indexes all with a value of 0
REDIM SHARED b%(30) 'if activated, the program works.
' CLEAR will now remove this dynamic array
CLEAR
' this loop will create a new dynamic array at first and then populate it
FOR i% = 1 TO 40
' when i% = 1 the code creates a new instance of b%() with a dimension of 1 ( b%(1) )
' when i% > 1 the code resizes the existing array ( b%(i%) ) and preserves the data previously inserted
REDIM _PRESERVE b%(i%)
b%(i%) = i%
NEXT i%
CALL ciao
SUB ciao
PRINT b%(3)
END SUB