Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Bitwise Enum and the <Flags()> [modified]

Bitwise Enum and the <Flags()> [modified]

Scheduled Pinned Locked Moved Visual Basic
htmlquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alsvha
    wrote on last edited by
    #1

    I'm messing about a bit with Enums and combining them via (bitwise) OR and AND so I can assign multiple values to the Enum-types, but I am having some conceptual problems. I noticed in the msdn documentation that there was a attribute which could be set to your enum. If I look at the msdn for it states: Indicates that an enumeration can be treated as a bit field; that is, a set of flags. However my code seems to be indifferent to whether i assign to my Enum or not, hence my confusion? It seems to be able to handle both the OR and the AND with and without . Can anybody give a quick explanation or point me to an article/ressource which explains this? Thanks in advance :) --------------------------- 127.0.0.1 - Sweet 127.0.0.1 -- modified at 4:13 Friday 29th September, 2006 forgot to ignore html tags :o

    D 1 Reply Last reply
    0
    • A Alsvha

      I'm messing about a bit with Enums and combining them via (bitwise) OR and AND so I can assign multiple values to the Enum-types, but I am having some conceptual problems. I noticed in the msdn documentation that there was a attribute which could be set to your enum. If I look at the msdn for it states: Indicates that an enumeration can be treated as a bit field; that is, a set of flags. However my code seems to be indifferent to whether i assign to my Enum or not, hence my confusion? It seems to be able to handle both the OR and the AND with and without . Can anybody give a quick explanation or point me to an article/ressource which explains this? Thanks in advance :) --------------------------- 127.0.0.1 - Sweet 127.0.0.1 -- modified at 4:13 Friday 29th September, 2006 forgot to ignore html tags :o

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Alsvha wrote:

      However my code seems to be indifferent to whether i assign to my Enum or not,

      It would help if we could see your code and what you're trying to do! Combining flags is always done with an OR, not an AND. To check to see if a bit is turned on, you use AND:

      Public Class Form1
       
      <Flags()> Public Enum AccessOptions As Byte
      None = 0
      Read = 1
      Write = 2
      Delete = 4
      Query = 8
      Sync = 16
      End Enum
       
      Private Sub Button1_Click(blah, blah) Handles Button1.Click
      Dim MyOptions As AccessOptions
       
      ' Turn on multiple options
      MyOptions = AccessOptions.Read Or AccessOptions.Write Or AccessOptions.Sync
       
      ' Now check for each option being turned on. There are a few different ways
      ' of doing this!
      Debug.WriteLine("None : " & (MyOptions = AccessOptions.None) )
       
      Debug.WriteLine("Read : " & ((MyOptions And AccessOptions.Read) > 0) )
      Debug.WriteLine("Write : " & ((MyOptions And AccessOptions.Write) > 0) )
      Debug.WriteLine("Delete: " & ((MyOptions And AccessOptions.Delete) > 0) )
       
      Debug.WriteLine("Query : " & ((MyOptions And AccessOptions.Query) = AccessOptions.Query) )
      Debug.WriteLine("Sync : " & ((MyOptions And AccessOptions.Sync) = AccessOptions.Sync) )

      Dave Kreskowiak Microsoft MVP - Visual Basic

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Alsvha wrote:

        However my code seems to be indifferent to whether i assign to my Enum or not,

        It would help if we could see your code and what you're trying to do! Combining flags is always done with an OR, not an AND. To check to see if a bit is turned on, you use AND:

        Public Class Form1
         
        <Flags()> Public Enum AccessOptions As Byte
        None = 0
        Read = 1
        Write = 2
        Delete = 4
        Query = 8
        Sync = 16
        End Enum
         
        Private Sub Button1_Click(blah, blah) Handles Button1.Click
        Dim MyOptions As AccessOptions
         
        ' Turn on multiple options
        MyOptions = AccessOptions.Read Or AccessOptions.Write Or AccessOptions.Sync
         
        ' Now check for each option being turned on. There are a few different ways
        ' of doing this!
        Debug.WriteLine("None : " & (MyOptions = AccessOptions.None) )
         
        Debug.WriteLine("Read : " & ((MyOptions And AccessOptions.Read) > 0) )
        Debug.WriteLine("Write : " & ((MyOptions And AccessOptions.Write) > 0) )
        Debug.WriteLine("Delete: " & ((MyOptions And AccessOptions.Delete) > 0) )
         
        Debug.WriteLine("Query : " & ((MyOptions And AccessOptions.Query) = AccessOptions.Query) )
        Debug.WriteLine("Sync : " & ((MyOptions And AccessOptions.Sync) = AccessOptions.Sync) )

        Dave Kreskowiak Microsoft MVP - Visual Basic

        A Offline
        A Offline
        Alsvha
        wrote on last edited by
        #3

        Hey. Thanks for the reply, but my issue is that it doesn't seem to matter if I use the "" or not: Public Enum AccessOptions As Byte None = 0 Read = 1 Write = 2 Delete = 4 Query = 8 Sync = 16 End Enum seems to work like Public Enum AccessOptions As Byte None = 0 Read = 1 Write = 2 Delete = 4 Query = 8 Sync = 16 End Enum for my bitwise comparison and funtionality. Thus me being somewhat puzzled. --------------------------- 127.0.0.1 - Sweet 127.0.0.1

        D 1 Reply Last reply
        0
        • A Alsvha

          Hey. Thanks for the reply, but my issue is that it doesn't seem to matter if I use the "" or not: Public Enum AccessOptions As Byte None = 0 Read = 1 Write = 2 Delete = 4 Query = 8 Sync = 16 End Enum seems to work like Public Enum AccessOptions As Byte None = 0 Read = 1 Write = 2 Delete = 4 Query = 8 Sync = 16 End Enum for my bitwise comparison and funtionality. Thus me being somewhat puzzled. --------------------------- 127.0.0.1 - Sweet 127.0.0.1

          D Offline
          D Offline
          darkelv
          wrote on last edited by
          #4

          How do you represent both Read and Write allowed in your second example?

          A 1 Reply Last reply
          0
          • D darkelv

            How do you represent both Read and Write allowed in your second example?

            A Offline
            A Offline
            Alsvha
            wrote on last edited by
            #5

            I have made this example to mess about with it, which was where I found this issue: (please ignore bad naming as it is just a test project :)) I have this enum: Public Enum MyType As Integer TypeA = 1 TypeB = 2 TypeC = 4 TypeD = 8 TypeE = 16 End Enum (withouth <Flags()> ) I then "OR" some choices together and place them in a variable of the type of MyType. This then results in some interger value depending on the choices. Then I can check if a type is added to my object: Dim result As MyType = mEnuType And examineType Return result = examineType where mEnuType is the combined type variable and examineType is the one I try to check if contains. I can remove added types by using XOR and all that, and all seems to work flawless. Withouth using the <Flags()> with my Enum, hence my confusion. If I read the documentation (msdn) for <Flags()> it seems to indicate that I need it to do these bitwise functionalities, however this testing seems to indicate I do not, and I can't figure out what I am doing wrong?

            --------------------------- 127.0.0.1 - Sweet 127.0.0.1

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups