• Advertisement
Sign in to follow this  

[.net] (Enum)string

This topic is 4436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement