05-05-2024, 04:43 AM
(05-05-2024, 03:44 AM)CharlieJV Wrote: In the structured BASIC implementations, a variable referenced as A% is a variable distinct from variable "A" declared with "DIM A AS INTEGER", because the variable name "A%" is not the same as the variable name "A".
Go back and try it again in QB45 and previous versions of BASIC.
DIM a AS INTEGER
a% = 1
PRINT a
This will print the value of 1, as "a" is referencing the variable named "a", that is an INTEGER, while a% is *also* referencing the variable named "a" that is an INTEGER. In this case, "a%" is the same variable as "a".
In BASIC, variables are broken down into two distinct identifiers -- variable name and variable type.
a% is the variable a, which is an integer.
a! is the variable a, which is a single.
a -- without any suffix -- is a shortcut for whatever type it currently is working with.
Code: (Select All)
a% = 1
a! = 2.2
a$ = "cat"
Print a 'default is SINGLE, so this is 2.2
DefInt A
Print a 'default is now INTEGER, so a is now a% or 1
DefStr A
Print a 'default is now string, so a is now a$, or "cat"
Now, how does DIM play into all this?
DIM defines the variable-with-no-suffix as one specific type, sets that type, and it is forevermore that type. Changing the default type does nothing to change what that variable-with-no-suffix is. DIM a AS LONG, and a is forevermore a LONG, no matter if you DEFINT, DEFSTR, DEFLNG, or whatever!
But here's the deal at the end of the day:
1) A variable name can be used with *ALL* variable types, as long as you manually specify the suffix with that variable.
2) DIM variable name AS type, is just a shortcut to say, "this name is always going to be this type", so you don't have to type that suffix.
3) There is ZERO difference in the variable-without-a-suffix and the variable-with-a-suffix-of-the-same-type.
4) If you're crazy, feel free to use every variable with the same name and with different types, if you want to. Just don't forget the suffix, or to swap DEF types as wanted until you obfusicate your code into meaningless drivel that nobody would ever want to read/work on.