Cast string as variable? How?
How can i convert string value to a variable
For example i have a string like;
String a="myvariable";
And now the real variable;
String myvariable;
Then;
a.CastAsVariable()="hello";
How can i do that....
As i remember Flash action scripting has eval function for this.
How can i do this with java?
thank you
Message was edited by:
netsonicc
[420 byte] By [
netsonicca] at [2007-11-27 10:32:51]

String a="myvariable";
String myvariable;
myvariable = a;
There's no methods or evals needed. A String is a String.
Of course, if you don't need the temp variable 'a', just do this:
String myvariable ="myvariable";
You can't do that in Java. You can achieve more or less the same result with Maps.
http://java.sun.com/docs/books/tutorial/collections/
jverda at 2007-7-28 18:19:12 >

The closes thing comes to my mind is Reflection
yue42a at 2007-7-28 18:19:12 >

> The closes thing comes to my mind is Reflection
Almost certainly not appropriate here.
jverda at 2007-7-28 18:19:12 >

well, I was thinking if there is a variable with the string's name already defined, then you can get it using reflection. Yeah, you can't really create a variable out of blue without actually, huh, creating a variable.
yue42a at 2007-7-28 18:19:12 >

> well, I was thinking if there is a variable with the
> string's name already defined, then you can get it
> using reflection. Yeah, you can't really create a
> variable out of blue without actually, huh, creating
> a variable.
And you can only do that with member variables, not locals.
And when this question is asked, it's almost always in the context of wanting to "name variables on the fly," which means a Map, or, if the names will be "var1", "var2", "var3", etc., then an array or List.
jverda at 2007-7-28 18:19:12 >

If you're coming from a scripting background, I suggest forgetting everything and starting fresh, rather than dragging bad habits into Java.
private String Example() {
//Some calculation first
result1=calculation;
}
I call Example() method many times from the main method....And result1 must change like result2,result3 for different method calls...
So i am thinking about to send result1,result,result3 assignment variable as a method parameter like;
private String Example(String result) {
//Some calculation first
result.CastAsVariable=calculation;
}
and call it from main like
Example("result1");
Example("result2");
How many times i call this function also differs servlet call by servlet call so i cant use array and dont want to!
So this is why i was looking for a function like eval
Thanks for everyone
null
> And result1 must change like result2,result3 for different method calls...
No, the variable names don't need to change. Just store the results in an appropriate data structure, as has already been suggested.
~
Honestly, read reply #7 and burn it into your brain. I came to Java from Flash, and the transition was horrible, especially because Flash was my first programming language.
You could just append the result to an ArrayList if you want, by the way, or return the result, then append it, or put it into a Map, or whatever.
When you're trying to transition to Java from Flash, at least in my experiences, 9/10 times you think you can't do something it has an incredibly simple solution that you need to forget about Flash to understand.
thanks at all yeah Map is solved my problem.
My first programming language is Pascal :)... Flash was just an example;)
> thanks at all yeah Map is solved my problem.
If the "variable names" differ only by a number that is an index (v1, v2, v3...), then forget the Map and use a List or an array.
jverda at 2007-7-28 18:19:13 >
