To allow for different formatting options on a method that displays a news story, I've created an enumeration that can be passed in to specify how it gets displayed.
[Flags]
private enum NewsStyle
{
Thumbnail = 0,
Date = 1,
Text = 2,
Link = 4,
All = 8
}
string FormatNews( DataRow news, NewsStyle style )
{
StringBuilder HTML = new StringBuilder();
// Should the link be shown
if ( ((newsStyle & NewsStyle.All) == NewsStyle.All || (newsStyle & NewsStyle.Link) == NewsStyle.Link))
{
HTML.AppendFormat("<a style=\"text-decoration:none; color: rgb(66, 110, 171);\" href=\"ViewStory.aspx?nsid={0}\">",
UrlEncode(newsStory["NewsStoryID"].ToString()));
}
// Etc etc...
}
// So to call the method...
Response.Write( FormatNews( news, NewsStyle.Date | NewsStyle.Text ) );
The problem is that I can only get the code to work if I manually specify the values on the enum, otherwise the bitwise enum checking operation doesn't work correctly.
I've always followed the rule of let .net handle the assigning of values to enums - is this a geniuine exception?