views:

113

answers:

2

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?

+6  A: 

Yes, it is a genuine exception. Enumerations are by default assigned values from 0 onwards, each one 1 higher than the previous value, regardless of any FlagsAttributes. (There of course are more elaborate rules for when you specify some values manually – see the MSDN docs)

While it does somewhat make sense to have a flags enumeration automatically get values in powers of two, I see reasons why it's probably best not to do so:

It's perfectly reasonable to have an enum like this:

[Flags]
enum Foo
{
    None,
    Bar,
    Baz, 
    BarAndBaz
}

This of course requires the values to be 0, 1, 2 and 3 to be sensible.

It'd be pretty counter-intuitive to let the presence of a mere attribute change the entire meaning of a piece of C# code. Consider my Foo example enumeration. Should removing the Flags attribute be allowed to break the code by silently changing the enumerator values?

Joren
Thanks for the answer and good points on the reasoning
Peter Bridger
A: 

Yes, you have to specify the values in the Enum to get the added flag values to work correctly (to be able to combine two enum flags and not have that value conflict with another flag specified). You don't have to specify values to get it to compile, but the outcome will most likely not be the one you are looking for.

Kevin