Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

retrieve a variable that matches a string?  RSS feed

 
Max Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of "variable" are we talking about here? A field of an object?

Can you show us the code you have?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!