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

J

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
 
H

Herfried K. Wagner [MVP]

JB said:
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.
 
J

JB

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.

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
 

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

Top