Enumartion flags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jose Ines

    #1

    Enumartion flags

    Hello,
    Can anyone give me a link where I can find a tutorial on how to make
    enumaration flags? Not the structure (public enum MyFlags{...}) but how to
    asign the members values (0x01, 0x02) etc...

    Thanks.
    Jose.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Enumartion flags

    Jose,

    If you are going to use flag values, then you have to make sure that the
    values are zero and the powers of 2. So, your values would be:

    0
    1
    2
    4
    8
    16
    32
    64
    128
    etc
    etc

    Now when representing these with hex values, you will notice this:

    0x0001
    0x0002
    0x0004
    0x0008
    0x0010
    0x0020
    0x0040
    0x0080
    0x0100
    0x0200

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Jose Ines" <joseinesnospam @msn.com> wrote in message
    news:O4zCLb6pDH A.1724@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hello,
    > Can anyone give me a link where I can find a tutorial on how to make
    > enumaration flags? Not the structure (public enum MyFlags{...}) but how to
    > asign the members values (0x01, 0x02) etc...
    >
    > Thanks.
    > Jose.
    >
    >[/color]


    Comment

    • 100

      #3
      Re: Enumartion flags

      Hi Jose,

      [Flags]
      enum MyFlags
      {
      First = 0x01,
      Second = 0x02,
      Third = 0x04
      }

      [Flags] attribute is not necessary but it changes the way ToString() method
      works.

      HTH
      B\rgds
      100

      "Jose Ines" <joseinesnospam @msn.com> wrote in message
      news:O4zCLb6pDH A.1724@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hello,
      > Can anyone give me a link where I can find a tutorial on how to make
      > enumaration flags? Not the structure (public enum MyFlags{...}) but how to
      > asign the members values (0x01, 0x02) etc...
      >
      > Thanks.
      > Jose.
      >
      >[/color]


      Comment

      • 100

        #4
        Re: Enumartion flags

        Hi Nicholas,

        Flags doesn't have to be power of 2.

        B\rgds
        100

        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
        message news:eQfjLo6pDH A.2636@tk2msftn gp13.phx.gbl...[color=blue]
        > Jose,
        >
        > If you are going to use flag values, then you have to make sure that[/color]
        the[color=blue]
        > values are zero and the powers of 2. So, your values would be:
        >
        > 0
        > 1
        > 2
        > 4
        > 8
        > 16
        > 32
        > 64
        > 128
        > etc
        > etc
        >
        > Now when representing these with hex values, you will notice this:
        >
        > 0x0001
        > 0x0002
        > 0x0004
        > 0x0008
        > 0x0010
        > 0x0020
        > 0x0040
        > 0x0080
        > 0x0100
        > 0x0200
        >
        > Hope this helps.
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > "Jose Ines" <joseinesnospam @msn.com> wrote in message
        > news:O4zCLb6pDH A.1724@TK2MSFTN GP10.phx.gbl...[color=green]
        > > Hello,
        > > Can anyone give me a link where I can find a tutorial on how to make
        > > enumaration flags? Not the structure (public enum MyFlags{...}) but how[/color][/color]
        to[color=blue][color=green]
        > > asign the members values (0x01, 0x02) etc...
        > >
        > > Thanks.
        > > Jose.
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Jose Ines

          #5
          Re: Enumartion flags

          Thanks Nicholas,
          Do you know how to make a member represent several of the other
          members?

          public enum Border{
          None = 0x0000,
          Top = 0x0001,
          Right = 0x0002
          Left = 0x0004,
          Bottom = 0x0008
          All = ?
          }

          Thanks Again.
          Jose.

          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:eQfjLo6pDH A.2636@tk2msftn gp13.phx.gbl...[color=blue]
          > Jose,
          >
          > If you are going to use flag values, then you have to make sure that[/color]
          the[color=blue]
          > values are zero and the powers of 2. So, your values would be:
          >
          > 0
          > 1
          > 2
          > 4
          > 8
          > 16
          > 32
          > 64
          > 128
          > etc
          > etc
          >
          > Now when representing these with hex values, you will notice this:
          >
          > 0x0001
          > 0x0002
          > 0x0004
          > 0x0008
          > 0x0010
          > 0x0020
          > 0x0040
          > 0x0080
          > 0x0100
          > 0x0200
          >
          > Hope this helps.
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Jose Ines" <joseinesnospam @msn.com> wrote in message
          > news:O4zCLb6pDH A.1724@TK2MSFTN GP10.phx.gbl...[color=green]
          > > Hello,
          > > Can anyone give me a link where I can find a tutorial on how to make
          > > enumaration flags? Not the structure (public enum MyFlags{...}) but how[/color][/color]
          to[color=blue][color=green]
          > > asign the members values (0x01, 0x02) etc...
          > >
          > > Thanks.
          > > Jose.
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Enumartion flags

            It doesn't have to be, but it should be if you are going to make it work
            correctly.

            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "100" <100@100.com> wrote in message
            news:Ona8ss6pDH A.360@TK2MSFTNG P12.phx.gbl...[color=blue]
            > Hi Nicholas,
            >
            > Flags doesn't have to be power of 2.
            >
            > B\rgds
            > 100
            >
            > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
            in[color=blue]
            > message news:eQfjLo6pDH A.2636@tk2msftn gp13.phx.gbl...[color=green]
            > > Jose,
            > >
            > > If you are going to use flag values, then you have to make sure that[/color]
            > the[color=green]
            > > values are zero and the powers of 2. So, your values would be:
            > >
            > > 0
            > > 1
            > > 2
            > > 4
            > > 8
            > > 16
            > > 32
            > > 64
            > > 128
            > > etc
            > > etc
            > >
            > > Now when representing these with hex values, you will notice this:
            > >
            > > 0x0001
            > > 0x0002
            > > 0x0004
            > > 0x0008
            > > 0x0010
            > > 0x0020
            > > 0x0040
            > > 0x0080
            > > 0x0100
            > > 0x0200
            > >
            > > Hope this helps.
            > >
            > > --
            > > - Nicholas Paldino [.NET/C# MVP]
            > > - mvp@spam.guard. caspershouse.co m
            > >
            > > "Jose Ines" <joseinesnospam @msn.com> wrote in message
            > > news:O4zCLb6pDH A.1724@TK2MSFTN GP10.phx.gbl...[color=darkred]
            > > > Hello,
            > > > Can anyone give me a link where I can find a tutorial on how to[/color][/color][/color]
            make[color=blue][color=green][color=darkred]
            > > > enumaration flags? Not the structure (public enum MyFlags{...}) but[/color][/color][/color]
            how[color=blue]
            > to[color=green][color=darkred]
            > > > asign the members values (0x01, 0x02) etc...
            > > >
            > > > Thanks.
            > > > Jose.
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Guest's Avatar

              #7
              Re: Enumartion flags

              All = None | Top | Right | Left | Bottom

              | sets a flag
              & tests for it


              "Jose Ines" <joseinesnospam @msn.com> wrote in message
              news:esBT9w6pDH A.708@TK2MSFTNG P10.phx.gbl...[color=blue]
              > Thanks Nicholas,
              > Do you know how to make a member represent several of the other
              > members?
              >
              > public enum Border{
              > None = 0x0000,
              > Top = 0x0001,
              > Right = 0x0002
              > Left = 0x0004,
              > Bottom = 0x0008
              > All = ?
              > }
              >
              > Thanks Again.
              > Jose.
              >
              > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
              in[color=blue]
              > message news:eQfjLo6pDH A.2636@tk2msftn gp13.phx.gbl...[color=green]
              > > Jose,
              > >
              > > If you are going to use flag values, then you have to make sure that[/color]
              > the[color=green]
              > > values are zero and the powers of 2. So, your values would be:
              > >
              > > 0
              > > 1
              > > 2
              > > 4
              > > 8
              > > 16
              > > 32
              > > 64
              > > 128
              > > etc
              > > etc
              > >
              > > Now when representing these with hex values, you will notice this:
              > >
              > > 0x0001
              > > 0x0002
              > > 0x0004
              > > 0x0008
              > > 0x0010
              > > 0x0020
              > > 0x0040
              > > 0x0080
              > > 0x0100
              > > 0x0200
              > >
              > > Hope this helps.
              > >
              > > --
              > > - Nicholas Paldino [.NET/C# MVP]
              > > - mvp@spam.guard. caspershouse.co m
              > >
              > > "Jose Ines" <joseinesnospam @msn.com> wrote in message
              > > news:O4zCLb6pDH A.1724@TK2MSFTN GP10.phx.gbl...[color=darkred]
              > > > Hello,
              > > > Can anyone give me a link where I can find a tutorial on how to[/color][/color][/color]
              make[color=blue][color=green][color=darkred]
              > > > enumaration flags? Not the structure (public enum MyFlags{...}) but[/color][/color][/color]
              how[color=blue]
              > to[color=green][color=darkred]
              > > > asign the members values (0x01, 0x02) etc...
              > > >
              > > > Thanks.
              > > > Jose.
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • 100

                #8
                Re: Enumartion flags

                Hi,[color=blue]
                > It doesn't have to be, but it should be if you are going to make it[/color]
                work[color=blue]
                > correctly.[/color]

                What do you mean?
                I may have flag that uses more then one bit.

                for example one flag occupies the first two less significant bits (0 and
                1st) (flag1 = 0x3)
                then one flag that uses 3th and 5th bit let say (flag2 = 0x14).

                enum MyFlags
                {
                Flag1 = 0x3,
                Flag2 = 0x14
                }


                MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;

                Do you want to say that this is incorrect or this won't work correctly?
                Yes, I agree that usually one flag uses only one bit, but not always. There
                is nothing wrong to have one flag represented by more then one bit. It will
                work correctly as long as the semantic of those flags is correct.

                B\rgds
                100


                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: Enumartion flags

                  Ok, in that case, it will work. However, this is not the common case,
                  but you are right. The only drawback to this is that you limit yourself to
                  the number of values that you can contain in your enumeration which can be
                  effective bit mappings (the max you can have is 32 right now).

                  --
                  - Nicholas Paldino [.NET/C# MVP]
                  - mvp@spam.guard. caspershouse.co m

                  "100" <100@100.com> wrote in message
                  news:O$UNbA7pDH A.1488@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > Hi,[color=green]
                  > > It doesn't have to be, but it should be if you are going to make it[/color]
                  > work[color=green]
                  > > correctly.[/color]
                  >
                  > What do you mean?
                  > I may have flag that uses more then one bit.
                  >
                  > for example one flag occupies the first two less significant bits (0 and
                  > 1st) (flag1 = 0x3)
                  > then one flag that uses 3th and 5th bit let say (flag2 = 0x14).
                  >
                  > enum MyFlags
                  > {
                  > Flag1 = 0x3,
                  > Flag2 = 0x14
                  > }
                  >
                  >
                  > MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;
                  >
                  > Do you want to say that this is incorrect or this won't work correctly?
                  > Yes, I agree that usually one flag uses only one bit, but not always.[/color]
                  There[color=blue]
                  > is nothing wrong to have one flag represented by more then one bit. It[/color]
                  will[color=blue]
                  > work correctly as long as the semantic of those flags is correct.
                  >
                  > B\rgds
                  > 100
                  >
                  >[/color]


                  Comment

                  • 100

                    #10
                    Re: Enumartion flags

                    When it goes for flags we are limited anyway by the underlying type of the
                    enumerations.
                    The ony reason for bringing this is because from the posts releated to
                    using enumerations as flags, I've seen, I got the impression that a lot of
                    programmers think that if they specify [Flags] attributes the values for the
                    enum's items *have to be* power of 2 otherwise they won't work.

                    B\rgds
                    100

                    "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                    message news:exP46D7pDH A.1960@TK2MSFTN GP12.phx.gbl...[color=blue]
                    > Ok, in that case, it will work. However, this is not the common case,
                    > but you are right. The only drawback to this is that you limit yourself[/color]
                    to[color=blue]
                    > the number of values that you can contain in your enumeration which can be
                    > effective bit mappings (the max you can have is 32 right now).
                    >
                    > --
                    > - Nicholas Paldino [.NET/C# MVP]
                    > - mvp@spam.guard. caspershouse.co m
                    >
                    > "100" <100@100.com> wrote in message
                    > news:O$UNbA7pDH A.1488@TK2MSFTN GP12.phx.gbl...[color=green]
                    > > Hi,[color=darkred]
                    > > > It doesn't have to be, but it should be if you are going to make[/color][/color][/color]
                    it[color=blue][color=green]
                    > > work[color=darkred]
                    > > > correctly.[/color]
                    > >
                    > > What do you mean?
                    > > I may have flag that uses more then one bit.
                    > >
                    > > for example one flag occupies the first two less significant bits (0 and
                    > > 1st) (flag1 = 0x3)
                    > > then one flag that uses 3th and 5th bit let say (flag2 = 0x14).
                    > >
                    > > enum MyFlags
                    > > {
                    > > Flag1 = 0x3,
                    > > Flag2 = 0x14
                    > > }
                    > >
                    > >
                    > > MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;
                    > >
                    > > Do you want to say that this is incorrect or this won't work correctly?
                    > > Yes, I agree that usually one flag uses only one bit, but not always.[/color]
                    > There[color=green]
                    > > is nothing wrong to have one flag represented by more then one bit. It[/color]
                    > will[color=green]
                    > > work correctly as long as the semantic of those flags is correct.
                    > >
                    > > B\rgds
                    > > 100
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Kerry Sanders

                      #11
                      Re: Enumartion flags

                      > Ok, in that case, it will work. However, this is not the common case,[color=blue]
                      >but you are right. The only drawback to this is that you limit yourself to
                      >the number of values that you can contain in your enumeration which can be
                      >effective bit mappings (the max you can have is 32 right now).[/color]


                      Actually, you are limited to the type of your enum. By default, being an int,
                      that would be 32, but you can also just as easily define the following if you do
                      not need to spend 4 bytes of space. But then again, what is space. :)

                      public enum MyFlags : short
                      {
                      None = 0,
                      First = 1,
                      Second = 2,
                      Third = 4
                      }


                      .... and so on. In this case, the maximum effective bit mappings would be 16.

                      But then, I know that you knew this. I figured I would point out that the type
                      of an enum could be declared as well. I did not know this until last night.

                      Comment

                      Working...