Re: Boolean operations on Enumerations
- From: Shawnk <Shawnk@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 7 Jun 2006 08:48:02 -0700
Nicholas,
Just so you know. I'm not checking for type and value in these examples.
In the 'real code' I do all the type checking and value checking
(Enum.IsDefined).
I can do the following -
Enum m_state;
m_state = New_value;
but to add 'additional states' as in;
m_state = m_state | New_value;
m_state = m_state | New_value_01 | New_value_02;
are the assignment semantics I was thinking of.
Thanks so much for your help on this :-)
Shawnk
"Nicholas Paldino [.NET/C# MVP]" wrote:
Shawnk,.
Assignment expressions require a type that is the same, or can be
implicitly cast (if there is no cast) or an explicit cast. You don't need
the if statement (and I dont know how that would work if you did).
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Shawnk" <Shawnk@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:53C3339B-C6B6-4FC6-8762-E93C930337D6@xxxxxxxxxxxxxxxx
A clarification. I meant for assigment of values not testing.
I understand for the testing the enumeration state as in;
if ( target_state & desired_value != 0)
{
// Do something because the target state has the desired value
}
what about a bit wise OR for assignment (is what I meant) as in;
target_state += additional_value;
target_state = target_state | additional_value;
Will these expressions need the 'if()' context to work?
Thanks much for your help.
Shawnk
"Shawnk" wrote:
Interesting.
Do you know the reasoning behind the boolean expression semantics?
Why does C# have the '!=' and the 'if()' semantics when performing simple
boolean logic?
I ask just in case there is something I should be testing for or doing in
my
code.
Thanks so much for your response.
Shawnk
"Nicholas Paldino [.NET/C# MVP]" wrote:
Shawnk,
Actually, C# does have the &, +=, and | operators. It's just that
you
need to use an expression that returns a boolean value. That being
said,
you need to do this:
if (l_prt_sta & portState.Active != 0)
That should work.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Shawnk" <Shawnk@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:68B89AF4-262C-4654-802B-BD6EE2AB0800@xxxxxxxxxxxxxxxx
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?
- Follow-Ups:
- Re: Boolean operations on Enumerations
- From: Tom Porterfield
- Re: Boolean operations on Enumerations
- References:
- Boolean operations on Enumerations
- From: Shawnk
- Re: Boolean operations on Enumerations
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Boolean operations on Enumerations
- From: Shawnk
- Re: Boolean operations on Enumerations
- From: Shawnk
- Re: Boolean operations on Enumerations
- From: Nicholas Paldino [.NET/C# MVP]
- Boolean operations on Enumerations
- Prev by Date: RE: what is the best way to make a DataTable thread safe? DataTable.Ac
- Next by Date: Re: system.drawing namespace question "Graphics"
- Previous by thread: Re: Boolean operations on Enumerations
- Next by thread: Re: Boolean operations on Enumerations
- Index(es):
Relevant Pages
|