Reputation: 1813
Is it possible to reference a variable/array using another variable in asp.net c#? EG.
alfaromeo = new string[] {"lots of stuff"};
astonmartin = new string[] {"lots of stuff"};
audi = new string[] {"lots of stuff"};
bentley = new string[] {"lots of stuff"};
bmw = new string[] {"lots of stuff"};
etc
string targetArray = "audi";
for(int i=0; i<targetArray.Length; i++){
// do stuff with tha array
}
There is a reason the arrays are all called different names and I'm not using a multi dimensional array to referance them.
Can this be done, or do I need to use some sort of look up array to reference them?
Thanks in advance.
Upvotes: 1
Views: 226
Reputation: 11903
No, not local variables.
I'd rather put the stuff in a dictionary:
var dict = new Dictionary<string, string[]>
{
{ "alfaromeo", new string[] {"lots of stuff"} }
// etc...
}
Or if you insist on variables, put them into fields of a class, and use reflection:
class Z {
public static string[] alfaromeo = new string[] {"lots of stuff"};
}
// ...
typeof(Z).GetField("alfaromeo", BindingFlags.Static | BindingFlags.Public).GetValue(null);
Upvotes: 2