Tuesday, July 3, 2007

Cursed Enums

When you're writing code using an object model, everyone likes to point out the obvious benefit of developing using enumerations.  The problem is that the database probably has certain values that represent the values of your enums.  How to convert between the values and the names?

public QuoteStatusType QuoteStatus
{
    get
    {
        return (QuoteStatusType)Enum.ToObject(typeof(QuoteStatusType),int.Parse(CurrentRow["QuoteStatus"].ToString()));
    }
    set
    {
        CurrentRow["QuoteStatus"] = (int)Enum.Parse(typeof(QuoteStatusType),value.ToString());
    }
}

Thanks C# Station

No comments: