02-03-2024, 04:35 PM
(This post was last modified: 02-03-2024, 04:50 PM by TerryRitchie.)
(02-03-2024, 09:52 AM)bartok Wrote:If a% were completely removed OPTION _EXPLICIT would flag the subsequent use of a% after the CLEAR statement as not being DIMensioned.(02-03-2024, 02:53 AM)TerryRitchie Wrote:Hi TerryRitchie. I take this opportunity in order to thank you for your tutorials that I used in 2020 in order to start learning QB64. I see that there are new lessons.(02-02-2024, 10:13 PM)bartok Wrote: Hi, I need an explanation about the command "CLEAR".CLEAR erases the values within variables. The variables still exist they are just nulled or set to zero.
DIM a% locates a part of memory for the variable a%, that is empty.
a%=1 give to the variable a% the value 1.
What does CLEAR? Does it makes a% empty again, or does it deletes the memory located for a%, virtually "deleting" the command DIM a%?
Dynamic arrays (REDIM) are completely removed however and will need to be redimensioned after a CLEAR statement. The code below shows the different behaviors.
Code: (Select All)OPTION _EXPLICIT
DIM a%
DIM s$
REDIM b%(20)
a% = 1
s$ = "Test"
b%(20) = 2
PRINT a%
PRINT s$
PRINT b%(20)
CLEAR
PRINT a% ' a reset to 0
PRINT s$ ' reset to null
'PRINT b%(20) ' if you activate this line you'll get an error
However, I was on the verge to ask you how do you know that CLEAR command doesn't remove variables set with DIM too, because for example:
PRINT a% 'with variable a% no set with DIM.
brings to the same result of:
DIM a%
a%=2
CLEAR
PRINT a%
This example make possible to think that CLEAR command might as well remove variable a%.
However:
DIM b%(30) 'like that the program works.
'REDIM b%(30) 'like that the program get an error.
b%(20) = 2
PRINT b%(20)
CLEAR
PRINT b%(20)
So I think that it could respond to my doubt.
But why CLEAR erase dynamic arrays and not set them to their initial dimension?
I make these questions because I have a program that has to be navigable by means of DO loops, and I have to chose what variables and arrays I can avoid to put into the first level of the DO loops, leaving them only on the start of the program, outside the DO loops, in order to don't repete uselessly their setting.
I'm glad you found the tutorials useful. If you used them in 2020 then you were probably still using the old tutorial site. A lot of lessons, new code, and changes have been made since then. There were also quite a few areas in the tutorial that needed clarification for better understanding. When you get time let me know what you think of the new tutorial site and with any suggestions you may have.