How to deconstruct the result of bitwise OR

  • Thread starter Thread starter Steve Long
  • Start date Start date
S

Steve Long

this is probably trivial, but, I just am not sure how to do it.

Say I have an argument for a routine that is an enumeration such as
moNone 0 'None
moLong 3 'Long
moDouble 5 'Double
moDate 7 'Date
moString 8 'String

This enum is defined in some other assymbly btw.

So, if I pass in say:
moLong Or moDouble

VB will bitwise Or these two enum members. How do I tell that it is those
two enumeration members? I can't just see if it equals 8 cause that is
another enum member.

I hope I have explained this clearly enough.

Thanks
Steve
 
Steve,
Unfortunately you cannot, as you have overlapping values.

If you are going to Or two Enum values together you need to ensure that each
value has a distinct value.

Such as:

moNone 0
moLong 1 << 0 ' 1
moDouble 1 << 1 ' 2
moDate 1 << 2 ' 4
moString 1<< 3 ' 8
End Enum

Then you can use Or as you have been to combine then, and use And to check
for a specific value.

Dim x As mo = moLong Or moDouble

If (x And moLong) <> 0 Then
' We have a Long
End If

If (x And moDouble) <> 0 Then
' We have a Double
End If

Const LongDouble As Mo = moLong Or moDouble
If (x And LongDouble) = LongDouble Then
' We have a Long
End If

Note: << & >> requires VB.NET 2003, the Flags attribute on Enum Mo allows
Enum.ToString to display the combined values.

Hope this helps
Jay
 
Steve Long said:
this is probably trivial, but, I just am not sure how to do it.

Say I have an argument for a routine that is an enumeration such as
moNone 0 'None
moLong 3 'Long
moDouble 5 'Double
moDate 7 'Date
moString 8 'String

This enum is defined in some other assymbly btw.

So, if I pass in say:
moLong Or moDouble

VB will bitwise Or these two enum members. How do I tell that it is those
two enumeration members? I can't just see if it equals 8 cause that is
another enum member.

I hope I have explained this clearly enough.


The enum is not set up for use as bit flags. To be used as flags, each
item has to have a value that is equal to some power of 2.

1, 2, 4, 8, 16, ...

The way it is, you won't always be able to determine which items were
included....

LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top