I lot of times in our Business Object Model we use enums, these enum property could be a string, integer or a short value. While retrieving records from the database we need to set the property in the BOM to that of the enum value.
Here is one of those methods that you can never find when you're looking for it
// suppose we have a enum
enum PublishingStatus : short
{
Draft = 1,
PendingApproval = 2,
Active = 3,
Archived = 4,
PendingDeletion = 5
}
//set a default value
PublishingStatusEnum CurrentPublishingStatus =
PublishingStatusEnum.Draft;
if (Enum.IsDefined(typeof(PublishingStatusEnum),
Helper.GetShort(Reader["CurrentPublishingStatusId"])))
CurrentPublishingStatus = (PublishingStatusEnum)Enum.Parse(
typeof(PublishingStatusEnum),
Reader["CurrentPublishingStatusId"].ToString());
// suppose we have a string enum
enum SiteColors
{
Red,
Blue,
Orange
}
//set a default value, pass the last variable as true of ignoreCase
SiteColors SelectedColor = SiteColors.Red;
if (Enum.IsDefined(typeof(SiteColors),
Reader["SiteColor"].ToString()))
SelectedColor = (SiteColors)Enum.Parse(
typeof(SiteColors), Reader["SiteColor"].ToString(),true);