Scrolling Game Development Kit Forum

SGDK Version 2 => Help, Errors, FAQ => Topic started by: bluemonkmn on 2011-12-27, 08:50:43 AM

Title: Converting integers and strings to Enumerated values
Post by: bluemonkmn 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 (http://msdn.microsoft.com/en-us/library/58918ffs(v=VS.100).aspx) and Enum.Parse (http://msdn.microsoft.com/en-us/library/essfb559.aspx).