4
In my program I have some classes where data is stored. For example
Class1.alfa.dado = 66;
Class1.beta.dado = 56;
Class1.gama.dado = 37;
The user will select in a Combobox one of the options you want to change, in this Combobox are the strings "alpha", "beta" and "gamma".
So I need to perform a function that does more or less like this
void change (string alterar)
{
Class1.(alterar).dado = 7;
}
How to use this "change" string to access the variable?
One solution would be to use the switch
. But the problem is that the functions are not so simple, they are large codes, and with the switch
It gets very repetitive and whenever I need to change something I have to move a lot of places. I would like to do this more automatically.
The problem is that there are not a few comparisons. This was just an example of the functionality I need so I don’t have to put in too much code.
– Antonio Rafael da Silva Filho
So I can’t help :(
– Gustavo Cinque
If I’m not mistaken, in Java you can also use Reflection to do the same thing, using the class
Field
: http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Field.html– OnoSendai