Bitwise Enum and the <Flags()> [modified]
-
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
-
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
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
-
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
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
-
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
-
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