Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 409771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:49:24+00:00 2026-05-12T17:49:24+00:00

To allow for different formatting options on a method that displays a news story,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:49:24+00:00Added an answer on May 12, 2026 at 5:49 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a route that can allow for different formats (html/json/xml
I have a cck field that needs to allow a different number of values
I have to create a program that would allow different users to post a
I'm trying to write a dynamic form for models that allow users with different
Enum.Format (or <enum>.ToString()) has an optional format parameter that allows you to specify different
I'm trying to modify an existing NSIS install script to allow for different licence
I'm on a different path on the same site, and I need to allow
I need to create interface MultiLingual, that allows to display object's data in different
To allow caching a PHP generated file, I want to make sure, that the
I am building a conditional formatting feature, where the user can enter an expression

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.