If a
, b
, and c
must stay the local int
s, the only way you do it is the boring one, i.e. like this:
int val;
if (TakeFrom == "a") {
val = a;
} else if (TakeFrom == "b") {
val = b;
} else if (TakeFrom == "c") {
val = c;
} else {
// It's an error
}
If you are open to some changes, there are several things that you could do:
- If you are willing to "encode" the
TakeFrom
, making it an integer instead of a string (say,0
meansa
,1
meansb
, and2
meansc
), you could placea
,b
, andc
into an array, and use the integerTakeFrom
as an index. - If you are willing to allow only single-character variables, you could replace the sequence of conditionals with a single
switch
. - If you are open to using standard collections, replace the individual variables with a map of string to integer.