Wednesday, September 12, 2007

Converting Enum instance to string for evaluation

I came across a situation where I needed to get a Querystring value and evaluate it against a Enum used for display modes.

// Display Mode Enum
public enum Mode

// Evalutate Querystring
object oDisplayMode = Request.QueryString["mode"];
if (oDisplayMode != null)
displayMode = (Mode)Enum.Parse(typeof(Mode), oDisplayMode.ToString(), true);

// Put into ViewState for next postback
ViewState["DisplayMode"] = displayMode;

No comments: