By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"JB" <jb*********@gmail.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

P: n/a
JB
On 4 Aug, 16:18, "Herfried K. Wagner [MVP]" <hirf-spam-me-h...@gmx.at>
wrote:
"JB" <jb.bross...@gmail.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 discussion thread is closed

Replies have been disabled for this discussion.