Data type. Multi pair of values. How can I do this?
-
Hello, I need to pass to a function a list of pairs of values. For each item the first value is a string and the second value is an Enum named Country. For example, consider I need to pass 3 items: "New York", Country.UnitedStates "Paris", Country.France "London", Country.England What data type should I use create this? And how can I access each item inside my function? Thanks, Miguel
-
Hello, I need to pass to a function a list of pairs of values. For each item the first value is a string and the second value is an Enum named Country. For example, consider I need to pass 3 items: "New York", Country.UnitedStates "Paris", Country.France "London", Country.England What data type should I use create this? And how can I access each item inside my function? Thanks, Miguel
For storing one object you can either use System.Web.UI.Pair or a custom class. The former takes two objects, so you must cast them apropriately after getting the value:
Pair p = new Pair("New York", Country.UnitedStates);
String s = (String)p.First;
Country c = (Country)p.Second;The latter can and should be typed, so no casting is necessary. You just define the members and create properties for them. Independent from your decision I also know two options, for storing multiple objects. One would be ArrayList, which again takes an object value, so casting is needed. It's still better, then a simple array, beacuse of the variable length. The second option would be a generic List, but that's only for .NET 2.0. It's typed, so no need for casting. Also you do the typing yourself.
List<Pair> pairs = new List<Pair>();
pairs.Add(new Pair("New York", Country.UnitedStates));
//Use your custom class here instead if Pair, if you decide, to do it that wayThere is a third option for storing your object, Dictionary, which also is a generic (and therefore a .NET 2.0 feature). It is based on key-value pair, where both of them are typed. But you probably don't need that.