Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Programming a "Bitwise" property

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Phil Jones - 30 Jul 2004 08:02 GMT
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
"Peter Huang" - 30 Jul 2004 09:56 GMT
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

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jay B. Harlow [MVP - Outlook] - 30 Jul 2004 13:35 GMT
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

> I'm wondering how you go about programming a property that accepts a bitwise
> combination of enum values.
[quoted text clipped - 9 lines]
> ===
> Phil : New Zealand
Phil Jones - 31 Jul 2004 00:27 GMT
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,
> As Peter showed you can use an enum. Most of the time if you are going to
[quoted text clipped - 24 lines]
> Hope this helps
> Jay
Jay B. Harlow [MVP - Outlook] - 31 Jul 2004 19:14 GMT
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

> Thanks Jay, that's really useful - particularly the use of the shift
> operator to keep the enum declaration looking readable.
[quoted text clipped - 43 lines]
> > Hope this helps
> > Jay
Phil Jones - 01 Aug 2004 23:06 GMT
Perject - that makes sense.  Simple when you know how!

Cheers,
Phil
Phil Jones - 17 Aug 2004 03:00 GMT
Man - this is proving to be sooooo useful.  Thanks Jay

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.