accessing a variable from "ex"+ i

Mar 27, 2006 at 4:46pm

accessing a variable from "ex"+ i

Hello,
i’m trying to do something which i normally do in flash actionscript very easily but didn’t manage to find out how to do it in java, it is easier to show an example:

private int ex1=1;
private int ex2=2;
private int ex3=3;
private int ex4=4;
private int ex5=5;

for(i=1;i<6;i++){
VALS[i]=”ex”+i;
}

my goal is that in the end my VALS array should be like this:
VALS[1]=1
VALS[2]=2
VALS[3]=3
VALS[4]=4
VALS[5]=5

instead i get this:
VALS[1]=ex1
VALS[2]=ex2
VALS[3]=ex3
VALS[4]=ex4
VALS[5]=ex5

how can i make java to interpret the “ex”+i and read the variable “ex1″, “ex2″…

thankx in advance

Best
ag

#25088
Mar 27, 2006 at 8:17pm

Hi Andre,

You can’t accomplish this in the way you’re approaching, due to the
static nature of Java compilation. Javascript and actionscript are
very different in this way from Java/C/C++, using dynamic binding at
runtime. So you’ll need to find a different solution (e.g. use an
array for your ex variables, store values in a hashtable which is
dynamically resolved at runtime, manually type all the code rather
than using a for loop, or some other approach).

-Joshua

#73438
Mar 27, 2006 at 9:10pm

On 27 Mar 2006, at 21:17, Joshua Kit Clayton wrote:

> You can’t accomplish this in the way you’re approaching, due to the
> static nature of Java compilation.

Actually, it could probably be achieved using the reflection API, but
it would be untidy and not very efficient. From the fragment of the
example given, it’s not clear to me why it’s necessary to do the
coding this way.

nick rothwell — composition, systems, performance — http://
http://www.cassiel.com

#73439
Mar 28, 2006 at 6:03pm

hi again,
thankx for the enlightment, at least now i now that it’s not possible. i allways find out a way to go around this but sometimes i just wish it could be possible.
the example i gave was not a real example, it was just to be simpler for you to help me, so it was easy to go around it…
best
ag

#73440

You must be logged in to reply to this topic.