We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › accessing class info by name
Page Index Toggle Pages: 1
accessing class info by name (Read 249 times)
accessing class info by name
Jan 11th, 2009, 4:58pm
 
Hi, I don't even know how to search for it.
I've got a class myClass with several vars in it var1, var2, var3...var100
I also have a file.txt in which I find something like var13 25.
What I want to do is read first char [var13] and so do myClass.var13=25.
As you can see I can't access the var directly as I don't know which one it will be and I'm trying to avoid switch.

In flash I'd do myClass[var], where var="var13", and it would work.

Can anybody help? Thanks!
Re: accessing class info by name
Reply #1 - Jan 11th, 2009, 5:16pm
 
You cant to this in JAVA, but you can use a Hashmap to store your values (http://processing.org/reference/HashMap.html)
Re: accessing class info by name
Reply #2 - Jan 11th, 2009, 5:22pm
 
Well, thanks but in fact this class is given to me through a library, so I can't turn it into a hashmap.
I'm trying to access to notes in jmEtude and I want to read a sequence, as C5 A4 G4. I'd like to read C5 and use e.C5, A4 and use e.A4, and so on.

Any other suggestion?
Re: accessing class info by name
Reply #3 - Jan 11th, 2009, 6:05pm
 
Mhmm, you can store all vars of your class in a hashmap and access them by the loaded string.

Code:

HashMap sequences= new HashMap();
sequences.put("C5 ", e.C5 );
sequences.get(varC5) = loadedvalue
Re: accessing class info by name
Reply #4 - Jan 11th, 2009, 11:21pm
 
While this is fundamentally non-OO, one can be more pragmatic in Processing than in Java...

To answer directly to the original request, you can use reflection, particularly the Class class, particularly the getDeclaredField method. (Not tried it!)
Page Index Toggle Pages: 1