Value of a variable named in a string

Is there any way to get the value of a variable that has been named in a string.
For instance:

Dim number As Integer = 12

Dim numberCopy As Integer = Convert("number")
' numberCopy will now equal 12
' Convert is the function to convert the string to the variable

Nothing built in

Curious about your usage for something like this as that may reveal some opportunities

I’m building a library of Linq type expressions to work on collections of objects.
At the moment, the Where function takes three parameters: The property, the comparison operator and the value. These are two strings and a variant… but I wanted to find a way to pass a single string, that can be parsed (using a regular expression) into the three elements. However, I wanted to be able to specify a variable by name…

…but, I have just realised the flaw in the plan (amazing what happens when you talk it through). The variable wouldn’t be passed through.
The best way, I guess would be to create the string with the value of the variable BEFORE passing it to the method.

The closest you could get to this in Xojo is by using a Dictionary with your “variable name” as the key and the variable’s value as the value.

Thanks Kem, unfortunately that would do what I wanted. In fact, what I was looking at isn’t really possible without re-writing the compiler, and I don’t think Xojo inc would do that just for my little experiment.

Brock did something like this
There might be other posts too

I remember seeing a post that someone else (Brock?) had already done this, I was attempting to replicate, and possibly improve, on the attempt.

[quote=264034:@Norman Palardy]Nothing built in

Curious about your usage for something like this as that may reveal some opportunities[/quote]

Xojoscript = script = text = string = what you could use to “eval” such.

Perhaps you could use Xojoscript to creat such system. I don’t see another way.

seems like a job for introspection?

Thats not going to be an efficient process but it might work. I think it would be better to refactor to use a dictionary to get and set the variables you want to access by name. But you could probably get it working with introspection.

Introspection works with objects, but not with strings or integers.

The propertyInfo class lets you access the properties, strings or integers, inside a class doesn’t it?

I don’t know if you can get to the global namespace or not, I don’t use introspection very much, but you can certainly walk through the list of variable names in a class and then pick the one you want and get it’s value.

You can’t use classic Introspection, but you can use the new framework Introspection. This works, for example:

dim t as text = "hi"
dim ti as Xojo.Introspection.TypeInfo = Xojo.Introspection.GetType( t )
MsgBox ti.Name // "Text"

Yup… I was looking for a Perl like “eval” operation. I have a file with a list of system commands. My UI collects info from the user like , serial number, IP-address, username, password, etc. In Perl I read a line like ${username}:${password}@${IPaddress}/${directory} and I run a “eval” to replace variable names with their values before I pass it along to the next routine. I was looking for some kind of built-in “eval” operation in XOJO. I can certainly build the URL from the internal variables, but I need to be able to read “text” from a file and make the appropriate substitutions. I think I’m going to have to roll my own with “find and replace” string operations.

take a look at the mathfield example here :

also this one to evaluate an expression :
it’s old realbasic, but certainly the source code is usefull and should not be difficult to update for xojo.

I’ll be the first to admit I may be missing something… but these examples center around mathematical evaluations…and I’m not certain these examples apply to string replacements. Certainly I can more or less simulate what I wanted to do by looking through lines and performing string replacements using a dictionary data type or something. I was just hoping there was an intrinsic class that would perform this operation. I think Xojo Script mentioned above might be an equally viable work around. I’d like to be able to fetch a value or property by a named string at runtime. More or less creating “dynamic variable names” Example: If var1 is a string type = “IPAdress” and var2 is a string = “Text” then I would like to do something like eval(var1+"."+var2) which would provide the value of IPAddress.Text which is perhaps a Textbox property.

then why not use xojoscript for that ?

An eval function is relatively easy to implement in an interpreted language, but not in compiled code.

Yes, I fear you’re going to have to refactor your expectations and find a different way to do what you want. You could certainly create a “getControlByName()” function that walked the control array for the window and found the one with the name you wanted and then cast it to the proper thing for use, but even that gets complicated and it’s probably easier to just access them directly. I don’t think you’re going to want to go through the acrobatics that would be necessary to pull such a thing off via xojoscript. You’d have to write a non-trivial amount of code into the context class to share enough data back and forth between the script and the actual program.

You are absolutely right. I sometimes forget when I cross over between a compiled and interpreted language. Plus…when Xojo code runs in the debugger it behaves like an interpreted language. Of course when you “build” the executable all the variable names disappear and are replaced by machine code / addresses etc. I will give Xojoscript a good look. I’ve never used it before and this would be a good time to add it to my solution toolbox. Thanks everyone.


I discovered yesterday that I had exactly the same need. It seems that I found a way, using “Introspection” :