Enum flags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Burland

    #1

    Enum flags

    I wanted to set up a enum to represent certain properties of an object that
    can be combined. So I'm using the FlagAttribute with my enum and adding them
    to by objects with | (or). But what is the right way to get them back (i.e.
    to check it this object has a certain flag set)? I tried:

    (MyObj.Properti es & MyProps.Prop1) == MyProps.Prop1

    But that doesn't work right. What gives?

    Thanks

    Matt


  • Mattias Sjögren

    #2
    Re: Enum flags

    Matt,
    [color=blue]
    >(MyObj.Propert ies & MyProps.Prop1) == MyProps.Prop1
    >
    >But that doesn't work right. What gives?[/color]

    In what way does it not work? How did you define your enum? What's the
    value of Prop1?



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • 100

      #3
      Re: Enum flags

      Hi Matt
      [color=blue]
      > (MyObj.Properti es & MyProps.Prop1) == MyProps.Prop1[/color]
      This is the right way to chack the flags.
      The problem I guess is in MyProps enum declaration.
      You have to defined like this

      [Flags]
      enum MyProps
      {
      Prop1 = 1,
      Prop2 = 2,
      Prop3 = 4,
      Prop4 = 8
      //and so on
      }

      Adding FalgsAttribute attribute doesn't set your enum values to be powers
      of 2. If you have overlapping flags that checking may not work.

      HTH
      B\rgds
      100


      Comment

      • Matt Burland

        #4
        Re: Enum flags

        Yeap, that was it. It just dawned on me a minute ago. Thanks for the
        response.

        "100" <100@100.com> wrote in message
        news:%234Ol2Gru DHA.556@TK2MSFT NGP11.phx.gbl.. .[color=blue]
        > Hi Matt
        >[color=green]
        > > (MyObj.Properti es & MyProps.Prop1) == MyProps.Prop1[/color]
        > This is the right way to chack the flags.
        > The problem I guess is in MyProps enum declaration.
        > You have to defined like this
        >
        > [Flags]
        > enum MyProps
        > {
        > Prop1 = 1,
        > Prop2 = 2,
        > Prop3 = 4,
        > Prop4 = 8
        > //and so on
        > }
        >
        > Adding FalgsAttribute attribute doesn't set your enum values to be powers
        > of 2. If you have overlapping flags that checking may not work.
        >
        > HTH
        > B\rgds
        > 100
        >
        >[/color]


        Comment

        Working...