Replies To: declaring an enum whose associated vals are string rather than int

Re: declaring an enum whose associated vals are string rather than int

Posted 15 January 2008 - 12:46 PM

That isn't possible in C#, you can use characters like 'A' which map to an integer, but full strings won't work. If you want to do this type of functionality, perhaps you should look at either constants or maybe a struct will work for your purpose.

And use the following to make it seem like you are accessing the name using its value.

// Dp.ToString() will then be the value "SEC". So you could use the 5 to access and return a string.
// Just think of it as flipping it around.
DatePart dp = (DatePart)Enum.Parse(typeof(DatePart), "5", true);