Re: Boolean operations on Enumerations

Tech-Archive recommends: Fix windows errors by optimizing your registry



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?







.



Relevant Pages

  • Re: Boolean operations on Enumerations
    ... Assignment expressions require a type that is the same, ... Do you know the reasoning behind the boolean expression semantics? ... enum types for a state machine design as in; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Boolean operations on Enumerations
    ... I understand for the testing the enumeration state as in; ... Do you know the reasoning behind the boolean expression semantics? ... enum types for a state machine design as in; ...
    (microsoft.public.dotnet.languages.csharp)
  • Simple enumerated state machine impossible in C#
    ... This post is intended to verify that true value semantics DO NOT EXIST for the ... Enum class (relative to boolean operations). ... I would like to design a simple enumerated state machine ... The state machine would support any enum type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Boolean operations on Enumerations
    ... the reason is that conditional expressions should evaluate to true ... Do you know the reasoning behind the boolean expression semantics? ... enum types for a state machine design as in; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Boolean operations on Enumerations
    ... If I want to do an assignment to a bitmapped enum do I still ... Also I tryed the code without the '' wrapers around the core boolean ... Usually, you want to do a comparison, not an assignment. ... Do you know the reasoning behind the boolean expression semantics? ...
    (microsoft.public.dotnet.languages.csharp)