FlagsAttribute

Good grief, here's one that passed me by.

Oftentimes, I'll define an enumeration designed for bitwise operations, e.g.

enum OperationType
{
    Insert = 1,
    Update = 2,
    Delete = 4,
    Select = 8
}

According to the Great FxCop, this method needs to have the FlagsAttribute attribute defined, i.e.

[Flags()]
enum OperationType
{
    Insert = 1,
    Update = 2,
    Delete = 4,
    Select = 8
}

6 Comments

  • Thanks for the tip.



    VB.NET equivalent (example):



    <Flags()> _

    Enum OperationType

    Insert = 1

    Update = 2

    Delete = 4

    [Select] = 8

    End Enum

  • What does it actually do? I've been using flag type enums for ages with no side effects (and no FlagsAttribute).

  • I've always wondered this myself until I took a look at the ROTOR source code. Apparently it has something to do with special design time support for a richer editing experience within VS.NET.

  • Yup, we hit up against that one too and now flag them all with this when we want to do but comparisons.

  • Quick question... how do you do a check once you've got a bit field value to see if a certain item is included in the list.



    i.e.



    <Flags()>_

    Public Enum AccessType

    Input = 1

    Output = 2

    Read = 4

    Write = 8

    End Enum



    how do I then do??



    Dim AT as AccessType = AccessType.Input Or AccessType.Read



    If AT = AccessType.Read Then

    ' do read code

    End If

    If AT = AccessType.Write Then

    ' do write code

    End If



    ????? Anyone know??

  • This will do it:

    <Flags()>_

    Public Enum AccessType

    Input = 1

    Output = 2

    Read = 4

    Write = 8

    End Enum



    how do I then do??



    Dim AT as AccessType = AccessType.Input Or AccessType.Read



    If AT And AccessType.Read Then

    ' do read code

    End If

    If AT And AccessType.Write Then

    ' do write code

    End If

Comments have been disabled for this content.