Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DIM - AS - VARIABLE TYPE likely bug
#17
(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. Wink

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. Tongue
Reply


Messages In This Thread
DIM - AS - VARIABLE TYPE likely bug - by bartok - 05-03-2024, 06:59 AM
RE: DIM - AS - VARIABLE TYPE likely bug - by SMcNeill - 05-05-2024, 04:43 AM



Users browsing this thread: 9 Guest(s)