I want to create a class (static?) that simply maps a name to a value (1 to 1). What's a clean way of doing something like this:
public static class FieldMapper
{
public static GetValue(string Name)
{
if (Name == "abc")
return "Value1";
if (Name == "def")
return "Value2";
}
}
I might be having a mind block today. I am unable to think of a clean solution for a simple problem like this :(
Edit: All the values are known at compile time (There is no uniqueness - different keys can map to same value). I shouldn't be creating a datastructure that adds values at runtime. Also, I would like to avoid using a XML file
Sounds like a job for a dictionary.
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("abc", "Value1");
values.Add("def", "Value2");
Console.WriteLine(values["abc"]); // Prints "Value1"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With