1

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.

2
  • string targetArray = "audi"; is this a String or a String[] ? Commented Oct 27, 2014 at 10:01
  • A string. This would be where a just store the name of the array I want to use, unless you are seeing something a lot more logical than I. Which is very possible :) Commented Oct 27, 2014 at 10:04

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry to be so dim, but if I use a dictionary, how do I reference them once stored?
dict["alfaeomeo"] The string becomes the key into the dictionary. Try here for some better help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.