Page 1 of 1

QB 4.0 ERROR: Length incorrect with certain string variables

Posted: Tue Jan 05, 2021 12:59 am
by mikefromca
I have confirmed this on two instances that an incorrect value is returned each time in both Qbasic 1.1 and QuickBasic 4.0. The code is this:

Code: Select all

DIM SHARED v as STRING * 100
PRINT LEN(want$) 'Prints 100 instead of 3!!!

FUNCTION want$
v$="123"
want$=v$
END FUNCTION
To make this bug happen, the variable name being set for the return value of the function (excluding dollar sign) must be the same as the one declared as a shared fixed string in the mainline program. In this case, I used v.

If shared is removed from DIM then the correct answer is returned.

I have not tested this on QuickBasic 4.5 or later.

Re: QB 4.0 ERROR: Length incorrect with certain string variables

Posted: Tue Jan 05, 2021 10:47 am
by MikeHawk
Fairly certain it's working as intended. Fixed-length strings will always return the same length, regardless of their content.

Code: Select all

DIM v AS STRING * 50
v = "Fifty"
PRINT "*";STRING$(50, "-");"*"
PRINT "*";v;"*"
Consider the following:

Code: Select all

DECLARE FUNCTION var% (variable%)
DIM SHARED dummy AS INTEGER

dummy = 50
PRINT var%(10)

FUNCTION var% (variable%)
var% = variable% + dummy%
END FUNCTION
You expected QB to make a difference between v (an explicitly defined, shared fixed-length string) and v$ (an implicit, local, variable-length string)? The shared variable has precedent. You could RTRIM it before obtaining the length (but then, you'd get the length of the temporary string created by RTRIM...)