LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

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.

-snip- (should I go to the doctor?) -snip-
Probably.

Use function form of VAR() VAR (3): Returns a reference to a variable This feature is undocumented VAR("Variable name")

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"

Thanks so much!!!

-snip- (should I go to the doctor?) -snip-
Probably.
you are probably right... I'm obsessed with programming!!!

VERY IMPORTANT: If a variable hasn't been explicitly declared or used anywhere yet, you can't VAR() it. VAR() cannot create variables, it can only access them.

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 1
I 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.

how do you
CALL VAR(1)?

You can't CALL the VAR keyword; CALL only allows you to use callable functions and commands, not keywords like PRINT, INC, USE etc.

ooooh. ok. Good thing I have another method... I could
PRGINS "VAR " + VARNAME$
USE 3
EXEC 3
would that work? Also, what is the average age of everyone here?(~15?)

ooooh. ok. Good thing I have another method... I could
PRGINS "VAR " + VARNAME$
USE 3
EXEC 3
would that work? Also, what is the average age of everyone here?(~15?)
Iā€™m 13.

I think I get it... if the function has no return values and only one argument, it cannot be
CALL 'ED

(AGE<14 AND AGE>13)==TRUE (sorry...)

I think I get it... if the function has no return values and only one argument, it cannot be
CALL 'ED
Not 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.

oooooooooh. Thanks!!!!!!!!!!!!!!!!!!!!!!!!!

I need a way to create a variable by a string and then access it via VAR(3)

There's no way to create more variables in SB, but you can store lots of values in a single string or array.

ooooooh 'Kay

I thought you could PRGINS it, and run it though. I think that's why I had run it and I checked if the variable exists and it returned false.