I have three enums, which – in combination – should identify a unique state of affairs.
A struct of Enums, some bit fields, others not
enum KeyCode
{
A,
B,
C,
D,
E
}
[Flags]
enum KeyModifiers
{
Empty = 0,
NoModifiers = 10000,
Shift = 20000,
Control = 40000,
Alt = 80000
}
enum KeyState
{
Empty = 0,
Down = 10000000,
Up = 20000000
}
I would like to implement a class which encompasses that state, and which would provide a Contains method, which would allow me to somehow treat the combination of these enums as a single value supporting bitwise operations.
But the current implementation does not work, I believe because some of the underlying enums are not bit fields.
Contains does not work due to KeyCode
class KeyEvent
{
public KeyEvent(KeyCode key, KeyState down = KeyState.Empty, KeyModifiers modifiers = KeyModifiers.Empty)
{
Key = key;
State = down;
Modifiers = modifiers;
}
public int Id
{
get
{
return (int)Key + (int)State + (int)Modifiers;
}
}
public KeyCode Key { get; set; }
public KeyState State { get; set; }
public KeyModifiers Modifiers { get; set; }
public bool Contains(KeyEvent comparable)
{
return (Id & comparable.Id) == comparable.Id;
}
}
How could I achieve my aim, which is to be able to compare the current values of those enums (this KeyEvent) with an input set of those enum values (as another KeyEvent) so as to determine whether the input values are contained within the current instance (but not necessarily equal to it) ?
- I could simply compare the enums separately, but is there a better solution?
- I do not want to create a fourth enum that combines all the other three in a bitwise manner, as KeyData does for KeyCode and modifiers
You would have to either define
KeyCodelike thisOr to calculate the combined value with