Author Topic: Converting integers and strings to Enumerated values  (Read 2044 times)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Converting integers and strings to Enumerated values
« on: 2011-12-27, 08:50:43 AM »
One more tip in response to a question from Pizzaman.  Since there doesn't seem to be much activity in these forums, I think it makes sense to at least post/discuss these so we have something to talk about :)

If you have a need to convert integer or string values into Key values (for use with IsKeyPressed), or SpriteState names into the enumerated value of the same name, C# does provide a means to do this.  See the code below for an example:

Code: [Select]
Key k1 = (Key)17;
Key k2 = (Key)Enum.Parse(typeof(Key), "D");
Sprites.Player.State playerState = (Sprites.Player.State)Enum.Parse(typeof(Sprites.Player.State), "Climbing");

For more information, see the MSDN pages about typeof and Enum.Parse.