0

I have my enum defined like this.

 public enum Places : long
  {
    World = (long)1,
    India = (long)23424848,
    USA = (long)23424977
  }

Now I get a string of value like 'India'. I want the corresponding value of the enumerator.

for instance if i get the string 'World'(or world - case insensitive ), I need the value 1 to be returned.

I tried this way:

long woeid = ((long)(typeof(Places)country)); 

this doesnot work.

Is there a simple way to do?

Venkat
  • 1,702
  • 2
  • 27
  • 47

1 Answers1

3

The method you want is Enum.Parse.

You would use it like this:

string country = "India";
Places myplace = (Places)Enum.Parse(typeof(Places), country);
long placeID = (long)Enum.Parse(typeof(Places), country);
Chris
  • 27,210
  • 6
  • 71
  • 92
  • I still get the value as India. I need the value which is '23424848' – Venkat Aug 14 '15 at 13:37
  • Is there a way to take rid of case-sensitiveness. The string India and india should return 23424848. The string World and world should return 1 ? – Venkat Aug 14 '15 at 13:42
  • I assume you missed the minor update I made to get a long out as well as the enum which should give you the number. As for the case-sensitivity I assume by now you've actually followed the link to the overloads of enum.parse so you could see the one that deals with case sensitivity? – Chris Aug 16 '15 at 16:18