Programming a "Bitwise" property

  • Thread starter Thread starter Phil Jones
  • Start date Start date
P

Phil Jones

I'm wondering how you go about programming a property that accepts a bitwise
combination of enum values.

That is, where you can specify to a property something like: Enum.Value1 Or
Enum.Value5

What I can't figure out is how you derive, from the code inside the
property, what values have been specified. Is this a mathematical algoritm
based on the enum values, or is there a class that can help out here?

Thanks everyone.
===
Phil : New Zealand
 
Hi Phil,

Here goes the code snippet.

Module Module1
Sub Main()
Dim o As New Class1
Dim v1 As Class1.TestEnum
Dim v2 As Class1.TestEnum
v1 = Class1.TestEnum.Value1
v2 = Class1.TestEnum.Value2
o.TestProg = v1 Or v2
Console.WriteLine(o.TestProg.ToString())
o.TestProg = Class1.TestEnum.Value1 Or Class1.TestEnum.Value2
Console.WriteLine(o.TestProg.ToString())
End Sub
End Module

Public Class Class1
Public Enum TestEnum As Integer
Value1 = &H10
Value2 = &H20
Value3 = &H30
End Enum
Private _TP As Integer
Public Property TestProg() As TestEnum
Get
Return _TP
End Get
Set(ByVal Value As TestEnum)
_TP = Value
End Set
End Property
End Class


Is this what you want? If you still have any concern, please feel free to
let me know.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Phil,
As Peter showed you can use an enum. Most of the time if you are going to
use an enum "Bitwise", it should be powers of 2, plus I would include the
Flags attribute to signify that it is going to be used "Bitwise". I will
include other values (such as 3) for masks or predefined combinations.

<Flags> Public Enum TestEnum As Integer
None = 0
Value1 = &H1
Value2 = &H2
Value3 = &H4
End Enum

I normally include None with a value of zero for no bits set.

With VS.NET 2003 I like to use the shift operator when defining the values,
as it shows the bit position and I don't need to remember my powers of 2 ;-)

<Flags> Public Enum TestEnum As Integer
None = 0
Value1 = 1 << 0 ' bit position 0
Value2 = 1 << 1 ' bit position 1
Value3 = 1 << 2 ' bit position 2
Value4 = 1 << 3 ' bit position 3
End Enum


Hope this helps
Jay
 
Thanks Jay, that's really useful - particularly the use of the shift
operator to keep the enum declaration looking readable.

Theoretically I can see how the resulting number that is handed to the
property (the sum of the enum values - powers of 2) could be examined as a
binary number, to determine whether a position (relating to one of the
flags) is either on or off.

Practically I can't see how to do this. Ideally I want to be able to ask
the question - is Value1 represented in the value passed to the property.

Maybe I'm missing the obvious!! Many thanks!

===
Phil
(Auckland | Aotearoa, New Zealand)
 
Phil,
Practically I can't see how to do this. Ideally I want to be able to ask
the question - is Value1 represented in the value passed to the property.
You can use And & Or to manipulate the bits.

Dim value As TestEnum
value = TestEnum.Value1
value = value Or TestEnum.Value2

If (value And TestEnum.Value1) = TestEnum.Value1 Then
' do something with the first bit being set
End If

If (value And TestEnum.Value2) <> 0 Then
' do something with the first bit being set
End If

I prefer the first as you can do:

Const mask As TestEnum = TestEnum.Value1 Or TestEnum.Value2

If (value And mask) = TestEnum.Value1 Then
' do something with the first bit being set
End If

Where out of Value1 & Value2 you only want Value1 to be on.

Hope this helps
Jay
 
Back
Top