Substract enum

  • Thread starter Thread starter Robin Theilade
  • Start date Start date
R

Robin Theilade

Hi,

I am having trouble substracting an enum "value" from an enum instance eg.:

Keys keyStates = new Keys();
keyStates |= Keys.Left;
keyStates |= Keys.Right
keyStates -= Keys.Left // here is the problem

I know that it doesn't work with -= but it does illustrate what it is that I
would like to do.

Please help me :)

Best Regards,

Robin Theilade
 
Thanks Rudy!

I think your answer is correct but I can't validate it since my test code
bugs me at the moment...can you see what is wrong? When I OR the current
keyState (Keys.Left) with Keys.Right it equals "Keys.Right" and not
"Keys.Left, Keys.Right" as I would expect..

using System;
using System.Windows.Forms;

namespace Test
{
public class StaticTest
{
static void Main()
{
Console.WriteLine( "- STATIC -" );

Keys keyStates = Keys.None;
Console.WriteLine( "Should be 'None': " + keyStates );
keyStates |= Keys.Left;
Console.WriteLine( "Added 'Left': " + keyStates );
keyStates |= Keys.Right;
Console.WriteLine( "Added 'Right': " + keyStates );
keyStates &= ~Keys.Right;
Console.WriteLine( "Substracted 'Right': " + keyStates );
Console.ReadLine();
}
}
}

Best Regards,

Robin Theilade
 
Thanks Rudy!

I think your answer is correct but I can't validate it since my test
code bugs me at the moment...can you see what is wrong? When I OR the
current keyState (Keys.Left) with Keys.Right it equals "Keys.Right" and
not "Keys.Left, Keys.Right" as I would expect..

using System;
using System.Windows.Forms;

namespace Test
{
public class StaticTest
{
static void Main()
{
Console.WriteLine( "- STATIC -" );

Keys keyStates = Keys.None;
Console.WriteLine( "Should be 'None': " + keyStates );
keyStates |= Keys.Left;
Console.WriteLine( "Added 'Left': " + keyStates );
keyStates |= Keys.Right;
Console.WriteLine( "Added 'Right': " + keyStates );
keyStates &= ~Keys.Right;
Console.WriteLine( "Substracted 'Right': " + keyStates );
Console.ReadLine();
}
}
}

The values Left and Right are not exclusive bit values, they are values
of keys. Left is 37, Right is 39 (IIRC). So or-ind and and-ing them into
keyState is not correct (IOW, keyStates is not the state of keys at all).
Only the top 16 of the 32 bits are used as exclusive modifier bits (for
Shift, Ctrl and Alt).

What are you trying to do?
 
Hi Rudy,

I am trying to maintain a list of keys that are pressed down, which I am
using for an animated character example I am doing.. For now I have tried
both a bool[] and a Hashtable to keep the list but I don't think that it is
a very nice way to maintain the list. So I read in the MSDN that the Keys
enumeration could be used as flags ("This enumeration has a FlagsAttribute
attribute that allows a bitwise combination of its member values.") and so I
thought that it would be possible. Any syggestions?

Best Regards,

Robin Theilade
 
Hi Robin,

If you want to use enum with OR/AND functionality
then you need to set FlagsAttribute for this enum.

But then max. 32 enum values (if i'm not wrong)
- key values in your case.

Regards

Marcin
 
Hi:

My guess is that you defined Keys as:
enum Keys {Left, Right}

So below is what happened in your original code:

Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates |= Keys.Left; // Now keyStates is 0 which is
Keys.Left (0|0 = 0)
keyStates |= Keys.Right // keyState is 1 (0|1 = 1) which is
Key.Right
keyStates -= Keys.Left // now keyState is still 1, 1-0 = 1,
which is Keys.Right.

You could try this:

[Flags] // indicating enum vars below can be treated using bit operations
enum Keys {Left=1, Right=2}

Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates = Keys.Left; // keyStates = 1;
keyStates |= Keys.Right // keyState=3 (Left, Right);
keyStates -= Keys.Left; // keyState=2. keyStates &= ~Keys.Left
works too.

HTH
 
Hi,

I didn't define the enum it is build in within the .NET framework named
System.Windows.Forms.Keys and is already marked with an Flags attribute..
(MSDN link:
http://msdn.microsoft.com/library/d...tml/frlrfsystemwindowsformskeysclasstopic.asp)
but even though Microsoft flagged it with a FlagsAttribute I can not OR the
values

Best Regards,

Robin Theilade


Polaris said:
Hi:

My guess is that you defined Keys as:
enum Keys {Left, Right}

So below is what happened in your original code:

Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates |= Keys.Left; // Now keyStates is 0 which is
Keys.Left (0|0 = 0)
keyStates |= Keys.Right // keyState is 1 (0|1 = 1) which is
Key.Right
keyStates -= Keys.Left // now keyState is still 1, 1-0 = 1,
which is Keys.Right.

You could try this:

[Flags] // indicating enum vars below can be treated using bit operations
enum Keys {Left=1, Right=2}

Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates = Keys.Left; // keyStates = 1;
keyStates |= Keys.Right // keyState=3 (Left, Right);
keyStates -= Keys.Left; // keyState=2. keyStates &= ~Keys.Left
works too.

HTH

Robin Theilade said:
Thanks Rudy!

I think your answer is correct but I can't validate it since my test code
bugs me at the moment...can you see what is wrong? When I OR the current
keyState (Keys.Left) with Keys.Right it equals "Keys.Right" and not
"Keys.Left, Keys.Right" as I would expect..

using System;
using System.Windows.Forms;

namespace Test
{
public class StaticTest
{
static void Main()
{
Console.WriteLine( "- STATIC -" );

Keys keyStates = Keys.None;
Console.WriteLine( "Should be 'None': " + keyStates );
keyStates |= Keys.Left;
Console.WriteLine( "Added 'Left': " + keyStates );
keyStates |= Keys.Right;
Console.WriteLine( "Added 'Right': " + keyStates );
keyStates &= ~Keys.Right;
Console.WriteLine( "Substracted 'Right': " + keyStates );
Console.ReadLine();
}
}
}

Best Regards,

Robin Theilade
 
At said:
Hi Robin,

If you want to use enum with OR/AND functionality
then you need to set FlagsAttribute for this enum.

This enum is an existing Windows.Forms enum, and the only flags it
contains are for the Shift, Ctrl and Alt keys. Have a look at the docs,
and the KeyCode and Modifiers value. KeyCode is 0x0000FFFF, Modifiers is
0xFFFF0000. These mask the key codes (lower 16 bits) and the modifiers
(upper 156 bits). Only the modifiers can be or-ed.
 
I see. I checked the link, Left is defined as 37 and Right is 39. 37|39=39.
I think the way Keys are defined does not allow you to use the bit-wise
operations; because there is no way to distinguish those 3 values: 37, 39,
37|39; unless the Keys were defined something like 1,2,4,8,16...

Robin Theilade said:
Hi,

I didn't define the enum it is build in within the .NET framework named
System.Windows.Forms.Keys and is already marked with an Flags attribute..
(MSDN link:
http://msdn.microsoft.com/library/d...tml/frlrfsystemwindowsformskeysclasstopic.asp)
but even though Microsoft flagged it with a FlagsAttribute I can not OR the
values

Best Regards,

Robin Theilade


Polaris said:
Hi:

My guess is that you defined Keys as:
enum Keys {Left, Right}

So below is what happened in your original code:

Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates |= Keys.Left; // Now keyStates is 0 which is
Keys.Left (0|0 = 0)
keyStates |= Keys.Right // keyState is 1 (0|1 = 1) which is
Key.Right
keyStates -= Keys.Left // now keyState is still 1, 1-0 = 1,
which is Keys.Right.

You could try this:

[Flags] // indicating enum vars below can be treated using bit operations
enum Keys {Left=1, Right=2}

Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates = Keys.Left; // keyStates = 1;
keyStates |= Keys.Right // keyState=3 (Left, Right);
keyStates -= Keys.Left; // keyState=2. keyStates &= ~Keys.Left
works too.

HTH

Robin Theilade said:
Thanks Rudy!

I think your answer is correct but I can't validate it since my test code
bugs me at the moment...can you see what is wrong? When I OR the current
keyState (Keys.Left) with Keys.Right it equals "Keys.Right" and not
"Keys.Left, Keys.Right" as I would expect..

using System;
using System.Windows.Forms;

namespace Test
{
public class StaticTest
{
static void Main()
{
Console.WriteLine( "- STATIC -" );

Keys keyStates = Keys.None;
Console.WriteLine( "Should be 'None': " + keyStates );
keyStates |= Keys.Left;
Console.WriteLine( "Added 'Left': " + keyStates );
keyStates |= Keys.Right;
Console.WriteLine( "Added 'Right': " + keyStates );
keyStates &= ~Keys.Right;
Console.WriteLine( "Substracted 'Right': " + keyStates );
Console.ReadLine();
}
}
}

Best Regards,

Robin Theilade

At 16:08:36, 13.03.2004, Robin Theilade wrote:

keyStates |= Keys.Left;
keyStates |= Keys.Right
keyStates -= Keys.Left // here is the problem

keyStates &= ~Keys.Left.

--
Rudy Velthuis

"A man can't get rich if he takes proper care of his family."
- Navaho saying.
 
I didn't define the enum it is build in within the .NET framework named
System.Windows.Forms.Keys and is already marked with an Flags
attribute.. (MSDN link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/h
tml/frlrfsystemwindowsformskeysclasstopic.asp) but even though
Microsoft flagged it with a FlagsAttribute I can not OR the values

This enum is a bit tricky. The lower part is the key code, the upper part
contains bits that indicate whether Shift, Control or Alt are pressed,
and only these upper bits can be or-ed. They key codes are masked by
Keys.KeyCode, and the upper bits (Shift, Ctrl, Alt) are masked by the
Keys.Modifiers value.
--
Rudy Velthuis

"I'll moider da bum."
-- Heavyweight boxer Tony Galento, when asked what he thought of William
Shakespeare
 
Hi Rudy,

It is weird that they mark the enum with a FlagsAttribute when only three of
the values can be used as flags. Oh well I'll find another solution. Thanks
for your time! :)

Best Regards,

Robin Theilade
 
Yeah I'm beginning to get the point. The FlagAttribute indicates that one or
more values may be OR'ed together. What a nice attribute :)

Well thanks for helping me!

Best Regards,

Robin Theilade

Polaris said:
I see. I checked the link, Left is defined as 37 and Right is 39. 37|39=39.
I think the way Keys are defined does not allow you to use the bit-wise
operations; because there is no way to distinguish those 3 values: 37, 39,
37|39; unless the Keys were defined something like 1,2,4,8,16...

Robin Theilade said:
Hi,

I didn't define the enum it is build in within the .NET framework named
System.Windows.Forms.Keys and is already marked with an Flags attribute..
(MSDN link:
http://msdn.microsoft.com/library/d...tml/frlrfsystemwindowsformskeysclasstopic.asp)
but even though Microsoft flagged it with a FlagsAttribute I can not OR the
values

Best Regards,

Robin Theilade


Polaris said:
Hi:

My guess is that you defined Keys as:
enum Keys {Left, Right}

So below is what happened in your original code:

Keys keyStates = new Keys(); // keyStates is initialized as "0",
which
is
Keys.Left
keyStates |= Keys.Left; // Now keyStates is 0 which is
Keys.Left (0|0 = 0)
keyStates |= Keys.Right // keyState is 1 (0|1 = 1) which is
Key.Right
keyStates -= Keys.Left // now keyState is still 1, 1-0 = 1,
which is Keys.Right.

You could try this:

[Flags] // indicating enum vars below can be treated using bit operations
enum Keys {Left=1, Right=2}

Keys keyStates = new Keys(); // keyStates is initialized as "0",
which
is
Keys.Left
keyStates = Keys.Left; // keyStates = 1;
keyStates |= Keys.Right // keyState=3 (Left, Right);
keyStates -= Keys.Left; // keyState=2. keyStates &= ~Keys.Left
works too.

HTH

Thanks Rudy!

I think your answer is correct but I can't validate it since my test code
bugs me at the moment...can you see what is wrong? When I OR the current
keyState (Keys.Left) with Keys.Right it equals "Keys.Right" and not
"Keys.Left, Keys.Right" as I would expect..

using System;
using System.Windows.Forms;

namespace Test
{
public class StaticTest
{
static void Main()
{
Console.WriteLine( "- STATIC -" );

Keys keyStates = Keys.None;
Console.WriteLine( "Should be 'None': " + keyStates );
keyStates |= Keys.Left;
Console.WriteLine( "Added 'Left': " + keyStates );
keyStates |= Keys.Right;
Console.WriteLine( "Added 'Right': " + keyStates );
keyStates &= ~Keys.Right;
Console.WriteLine( "Substracted 'Right': " + keyStates );
Console.ReadLine();
}
}
}

Best Regards,

Robin Theilade

At 16:08:36, 13.03.2004, Robin Theilade wrote:

keyStates |= Keys.Left;
keyStates |= Keys.Right
keyStates -= Keys.Left // here is the problem

keyStates &= ~Keys.Left.

--
Rudy Velthuis

"A man can't get rich if he takes proper care of his family."
- Navaho saying.
 
Hi Robin,
But the FlagsAttribute is already set by Microsoft for the
System.Windows.Forms.Keys enumeration and there is plenty more than 32
values, they might have made a mistake but I don't know enough about C# to
tell..

(Link to the ref:
http://msdn.microsoft.com/library/d...tml/frlrfsystemwindowsformskeysclasstopic.asp)

You're right. I found 183 different values... but i think that
binary exclusive values are a codes of: CTRL, SHIFT, and ALT.
Rest of values could be non exclusives.

Marcin
 
Hi Marcin,

Yes that is what Rudy says too, well thanks for your input! :)

Best Regards,

Robin Theilade
 
Back
Top