| Author |
retrieve a variable that matches a string?
|
Max Simpson
Ranch Hand
Joined: Sep 23, 2004
Posts: 30
|
|
I have an array of people's names. I also have variables with the same names as the strings in the array I mention above. Is there anyway to take the name strings I retrieve from the array and retrieve the variable of the same name?
|
 |
M Beck
Ranch Hand
Joined: Jan 14, 2005
Posts: 323
|
|
there might be a way, using reflection. but i'm not very practiced at that API, so i'll just point you at sun's tutorial on the subject. however, i can't help but think you probably really want to be using a Map of some sort instead of the data types you actually are using. i think that would accomplish the same end more easily. look them up in the API, and read the collections tutorial if you haven't already.
|
 |
Jeff Bosch
Ranch Hand
Joined: Jul 30, 2003
Posts: 804
|
|
|
Are you saying that you want the variable's value or the variable's label to be the same as the data in the array element? If you want the value, you could construct one of the Java collections, add each array element to the collection either directly or in the constructor, then use one of the collection methods to check for existence of the record. If you want to rename the variable, I don't think you can do that...
|
Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
What kind of "variable" are we talking about here? A field of an object? Can you show us the code you have?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: retrieve a variable that matches a string?
|
|
|