Get the value of a variable from its name
Root / Programming Questions / [.]
DFrostCreated:
I am making an Object - Oriented - Programming extension for SB. I need to get the value of a variable by using a string.For example:
"hello_there" and getting the value of the variable by the name of hello_there
Please help
Also, should I mention I'm grounded? I'm just writing this on my school computer and ill write code on a composition notebook. I mentally debug and run it(should I go to the doctor?)
As far as I know, there is no way to do it.
Use function form of VAR() VAR (3): Returns a reference to a variable This feature is undocumented VAR("Variable name")He is correct, but to expound on this a bit:
VAR HELLO$="ABC" PRINT VAR("HELLO$") 'read VAR("HELLO$")="DEF" 'write DIM WORDS$[0] PUSH WORDS$,"ABC" PRINT VAR("WORDS$")[0] 'array indexing VAR("WORDS$")[0]="DEF"
I thought of a way you can(possibly) create a variable by its name. there are two methods I have thought of:
(I don't know the syntax to 1.)
1.
CALL "VAR"2.
PRGINS "VAR "+VARNAME$:EXEC 1I have created the next TYPEOF too... It can tell the difference between an array and a string.It uses
VAR(3)to do it, though. I wish there was a SmileBASIC compiler on pc. I'll try to do it.
ooooh. ok. Good thing I have another method... I couldIām 13.PRGINS "VAR " + VARNAME$ USE 3 EXEC 3would that work? Also, what is the average age of everyone here?(~15?)
I think I get it... if the function has no return values and only one argument, it cannot beNot quite. SPPAGE meets the criteria you mentioned and you can call it. In many modern programming languages, there's keywords and there's functions. Keywords are baked into the language syntax and aren't the same as callable functions, since they have a purpose or behavior that otherwise wouldn't be possible. An obvious example would be the structure of an IF block or a WHILE loop, but things like CALL and USE are less obvious examples. Luckily the syntax highlighting tells you the difference. Callable functions are dark blue, and keywords are light blue. The reserved keywords are also listed somewhere in the manual.CALL 'ED