Jump to content
  • Advertisement
Sign in to follow this  
Thevenin

[.net] (Enum)string

This topic is 4671 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!