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
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.
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.
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.
I dont know if you can get to the global namespace or not, I dont 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 its value.
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.
also this one to evaluate an expression : http://slobasso.tripod.com/programming/rb.html
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.
Yes, I fear youre 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 its probably easier to just access them directly. I dont think youre going to want to go through the acrobatics that would be necessary to pull such a thing off via xojoscript. Youd 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.