[.net] (Enum)string

Started by
2 comments, last by Thevenin 18 years, 3 months ago
I thought you could do this in C#...
public enum EGameDev
{
     Why,
     Does,
     It,
     Not,
     Work
}

public EGameDev ThePeople = (EGameDev)"Work";
.. but it doesn't work. [depressed]
Advertisement
I think you have to use reflection in order to do that. It does work with integers, but you'll have to initialize the first value to 0 to be sure you'll be able to cast.

It does work on switch statements however:
switch (str){    case "case a":         break;    case "case b":         break;}


Toolmaker

The syntax your looking for is

System.Enum.Parse(typeof(Enum Type Name), str);

Its possible you could write a type converter/overloaded operator that would let you use that (EnumTypeName)"string" syntax but I'm not really into operator overloading so I can't advise you on that.
I'm not a fan of operator overloading either. Thanks for the syntax.

This topic is closed to new replies.

Advertisement