G
Guest
I would like to perform various boolean operations on bitmapped
(FlagsAttribute)
enum types for a state machine design as in;
-------------------
[FlagsAttribute]
enum portState {
Unknown,
Open,
Active,
Secure,
Requester_trace_requested,
Requester_trace_cutoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
Close
}
myEnum l_prt_sta;
.... // Bitwise AND to test port state
if ( l_prt_sta & portState.Active )
{
// Do an active port process
}
.... // Add new states to port state
l_prt_sta += Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
The operations above (&, +=, |) are not provided in C#.
Unfortunately the Enum class does not have the value semantics to manipulate
the
enumeration as as a state machine value.
I could use Convert.ToInt32, Convert.ToInt64, ect,
perform the boolean operation,
and then reconvert the value back.
Is there a better way to implement boolean operations and assignments on an
enum?
Thanks ahead of time for any responses.
Shawnk
PS. I know about BitArray and BitVector32 but I would like to use the 'named
state' semantics that enum provides. So I want the 'state' to have both
boolean
vector and enumeration semantics.
PPS. ALso what is the best way to do multiple enum value assignments?
(FlagsAttribute)
enum types for a state machine design as in;
-------------------
[FlagsAttribute]
enum portState {
Unknown,
Open,
Active,
Secure,
Requester_trace_requested,
Requester_trace_cutoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
Close
}
myEnum l_prt_sta;
.... // Bitwise AND to test port state
if ( l_prt_sta & portState.Active )
{
// Do an active port process
}
.... // Add new states to port state
l_prt_sta += Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
The operations above (&, +=, |) are not provided in C#.
Unfortunately the Enum class does not have the value semantics to manipulate
the
enumeration as as a state machine value.
I could use Convert.ToInt32, Convert.ToInt64, ect,
perform the boolean operation,
and then reconvert the value back.
Is there a better way to implement boolean operations and assignments on an
enum?
Thanks ahead of time for any responses.
Shawnk
PS. I know about BitArray and BitVector32 but I would like to use the 'named
state' semantics that enum provides. So I want the 'state' to have both
boolean
vector and enumeration semantics.
PPS. ALso what is the best way to do multiple enum value assignments?