473,770 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Generic" Enum as a parameter to set a flag in a Bit Field

JB
Hi All,

I'm pulling my hair over this and could really do with a bit of help.
I'm using various different enums as bit fields and I'd like to create
a set of generic Functions or Subs to manipulate the bitfields.

For instance:

<Flags()_
Enum ActionFlags As Byte
None = 0
Log = 1
Display = 2
End Enum

<Flags()_
Enum TypeFlags As Byte
None = 0
eInfo = 1
eWarning = 2
eError = 4
End Enum

Public Sub SetFlag(ByRef iBitFieldValue As [Enum], ByVal iBitToSet As
[Enum])
iBitFieldValue = (iBitFieldValue Or iBitToSet)
End Sub

I've tried various different forms of this Sub but I can't seem to be
able to pass a "generic" Enum as a parameter that would work for both
ActionFlags and TypeFlags.

Any help would be greatly appreciated.
Thanks
JB

Aug 4 '07 #1
2 3519
"JB" <jb*********@gm ail.comschrieb:
I'm pulling my hair over this and could really do with a bit of help.
I'm using various different enums as bit fields and I'd like to create
a set of generic Functions or Subs to manipulate the bitfields.

For instance:

<Flags()_
Enum ActionFlags As Byte
None = 0
Log = 1
Display = 2
End Enum

<Flags()_
Enum TypeFlags As Byte
None = 0
eInfo = 1
eWarning = 2
eError = 4
End Enum

Public Sub SetFlag(ByRef iBitFieldValue As [Enum], ByVal iBitToSet As
[Enum])
iBitFieldValue = (iBitFieldValue Or iBitToSet)
End Sub

I've tried various different forms of this Sub but I can't seem to be
able to pass a "generic" Enum as a parameter that would work for both
ActionFlags and TypeFlags.
The reason is that you are using a 'ByRef' parameter, but by extending the
value passed to the '[Enum]' parameter to the 'Enum' type it can take values
which cannot be represented in the target type which is returned in the
'ByRef' parameter.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 4 '07 #2
JB
On 4 Aug, 16:18, "Herfried K. Wagner [MVP]" <hirf-spam-me-h...@gmx.at>
wrote:
"JB" <jb.bross...@gm ail.comschrieb:
I'm pulling my hair over this and could really do with a bit of help.
I'm using various different enums as bit fields and I'd like to create
a set of generic Functions or Subs to manipulate the bitfields.
For instance:
<Flags()_
Enum ActionFlags As Byte
None = 0
Log = 1
Display = 2
End Enum
<Flags()_
Enum TypeFlags As Byte
None = 0
eInfo = 1
eWarning = 2
eError = 4
End Enum
Public Sub SetFlag(ByRef iBitFieldValue As [Enum], ByVal iBitToSet As
[Enum])
iBitFieldValue = (iBitFieldValue Or iBitToSet)
End Sub
I've tried various different forms of this Sub but I can't seem to be
able to pass a "generic" Enum as a parameter that would work for both
ActionFlags and TypeFlags.

The reason is that you are using a 'ByRef' parameter, but by extending the
value passed to the '[Enum]' parameter to the 'Enum' type it can take values
which cannot be represented in the target type which is returned in the
'ByRef' parameter.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Hi Herfried,

Thanks for your reply. I did notice that the ByRef had an influence,
and when I use ByVal and return the value with a Function I get the
Error: Operator 'Or' is not defined between 'System.Enum' and
'System.Enum'.

Public Function SetFlag(ByVal iBitFieldValue As [Enum], ByVal
iBitToSet As [Enum]) As [Enum]
Return (iBitFieldValue Or iBitToSet)
End Function

The only way I got it to work was by casting all my Enums to Integer,
but really what I'd like to achieve would be no casting (for total
genericity) or at least the casting done within my function. Do you
know how I could do that?

Thanks
JB

Aug 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3868
by: Vicente Nicolau | last post by:
hello, I have a problem when I preview or print a document in an ISO A0. I have an application which load an 10x10 image (bmp or jpg) and spread it in the A0 paper. When I make the print preview, it starts to draw (ev.Graphics.DrawImage) but after a few seconds an exception throws:
2
9199
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not a problem. .... So one would like to think. The reality is that delegates and event...
3
1678
by: Tom H. | last post by:
Newbie question, hopefully not one that will embarrass me. I'm reading Richter's Applied MS .NET Framework Programming and keep seeing snippets such as this: Int32 v = 5; Object o = v; Console.Writeline("{0}, {1}, {2}", o, o, o); in which it appears that code referencing an Object magically knows
0
1692
by: Prasana | last post by:
Hi EveryBody, I have one Problem When I am Creating the DNS Zone Using Resource Record using System Management. Iam Getting an Error Called "Generic Error". Iam Able to Create a Primary Zone File Using CreateZone Class. But How to create other files like SOA, MX, Reverse look Up & other files. Iam working on Win 2000 M/c.Kindly Help. -- Regards, Prasana Srinivasan Software Engineer
1
1340
by: jason | last post by:
Hello everyone, I'm trying to expand my understanding of the DataGrid control, and I'm specifically curious about the Command columns. I have successfully implemented an EditCommandColumn, but I was wondering, is it possible to create my own kind of Command Column, with my own defined buttons and behaviors? Can I use a TemplateColumn as the basis for this? If you need more information to answer this question, let me know and I'll try...
5
8865
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the created methods are not "registered" and only available through the DynamicMethod class. So a method lookup finds the origin property. A little test: public class DerivedClass : BaseClass {
4
2957
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172: returning address of local variable or temporary". In good old 'C', that would indicate that a 'static' was missing from the declaration of the returned value.
5
11129
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a number of warnings from the compiler like: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets : warning : Type library exporter warning processing 'Utility.Array1D`1, Test2003'. Warning: Type library exporter encountered a...
0
1528
by: epoxyparser | last post by:
Hi all, I work as a glue coder and often have to interface existing mechanisms with other platforms. Creating bridge code between command-line functionality and desktop GUIs takes up a lot of that definition, along with creating abstraction layers for older-style C code. I've been working with .net (almost exclusively with C++.net, just a bit of C#) to create end-user interfaces with a relatively good deal of success (I'm used to ansi/STL and...
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.