Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CLEAR command
#5
(02-03-2024, 02:48 PM)bartok Wrote: I dony' understand. Look at this code:

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?
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)
' 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
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
CLEAR command - by bartok - 02-02-2024, 10:13 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 02:53 AM
RE: CLEAR command - by bartok - 02-03-2024, 09:52 AM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 04:35 PM
RE: CLEAR command - by bartok - 02-03-2024, 09:11 PM
RE: CLEAR command - by bartok - 02-03-2024, 02:48 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 04:29 PM
RE: CLEAR command - by bartok - 02-03-2024, 06:26 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 05:22 PM
RE: CLEAR command - by bartok - 02-03-2024, 06:16 PM
RE: CLEAR command - by SMcNeill - 02-03-2024, 06:32 PM
RE: CLEAR command - by bartok - 02-03-2024, 08:20 PM
RE: CLEAR command - by SMcNeill - 02-03-2024, 10:24 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 10:55 PM
RE: CLEAR command - by bartok - 02-04-2024, 09:56 AM
RE: CLEAR command - by bplus - 02-03-2024, 06:41 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 06:48 PM
RE: CLEAR command - by SMcNeill - 02-04-2024, 12:17 PM
RE: CLEAR command - by bartok - 02-04-2024, 01:13 PM
RE: CLEAR command - by Dimster - 02-04-2024, 03:26 PM
RE: CLEAR command - by bplus - 02-04-2024, 04:16 PM



Users browsing this thread: 4 Guest(s)