Getting the value of a variable variable

New to FreeBASIC? Post your questions here.
wolfstar
Posts: 96
Joined: Nov 07, 2006 12:42

Getting the value of a variable variable

Postby wolfstar » Apr 19, 2007 1:52

This is a serious question, I have no idea how to do it.

How would I get something like the following to return 5

Code: Select all

dim as string a,b,c
a="mouse"
b="keys"
c=".x"
mouse.x = 5

? a+c
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Apr 19, 2007 3:31

Not possible, all variable names are lost at run-time. They're simply converted into memory addresses once your progam actually runs.
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » Apr 19, 2007 4:06

mouse.x refers to an undefined (user defined type?) variable. If "mouse.x" was a symbol that was associated with a value of 5, you would need to assign it to a string variable, then you could search the symbol with instring() to find its components. From this you could work out the value 5.
a+c concatenates the strings just like the operator a&c does.
Seriously, why might you want to do this? Would it not be easier just to

Code: Select all

Print 5
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Apr 19, 2007 4:38

Richard...You either are joking or missed the point completely with the last part (the code).
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » Apr 19, 2007 23:02

@pritchard: Neither, possibly both.
This attempt to apply run-time output of a program to the compile-time symbol table management hits the problem of reverse time travel. cha0s was quite correct.
Even if FreeBASIC was an interpreter, the syntax and structure of the language precludes such things.
After considering a few possibilities it appeared to be necessary to either write a program to generate and compile another program file or to implement some code to manage a symbol table based solution. My code resulted from the logical elimination of other possibilities.
A competent bureaucrat or politician will not hold an inquiry without first being certain of the required outcome. The client clearly specified the outcome. There was no other simple way. My code was intended not so much a joke as an admission of defeat.
wolfstar
Posts: 96
Joined: Nov 07, 2006 12:42

Postby wolfstar » Apr 23, 2007 4:49

Ended up doing this:

device(0).x
device(1).x

where 0 is mouse and 1 is keys
counting_pine
Site Admin
Posts: 6173
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Apr 23, 2007 12:56

Good solution. It's a lot more efficient than parsing strings.
dcromley
Posts: 1
Joined: Apr 15, 2007 3:17
Location: Wyoming, USA

Postby dcromley » Apr 23, 2007 17:11

So, wolfstar, did you get the "5" you wanted? How?
(Please post your final snippet.)

Return to “Beginners”

Who is online

Users browsing this forum: MSN [Bot] and 3 guests