C
C#2y ago
nut

what is this code snippet doing?

ive got this code snippet from a library im trying to update, and i have no idea what its doing:
private MouseButtons m_Set;

public ButtonSet()
{
m_Set = MouseButtons.None;
}

public void Add(MouseButtons element)
{
m_Set |= element;
}

public void Remove(MouseButtons element)
{
m_Set &= ~element;
}

public bool Contains(MouseButtons element)
{
return (m_Set & element) != MouseButtons.None;
}
private MouseButtons m_Set;

public ButtonSet()
{
m_Set = MouseButtons.None;
}

public void Add(MouseButtons element)
{
m_Set |= element;
}

public void Remove(MouseButtons element)
{
m_Set &= ~element;
}

public bool Contains(MouseButtons element)
{
return (m_Set & element) != MouseButtons.None;
}
8 Replies
nut
nutOP2y ago
it seems like its somehow using a single MouseButtons enum value as an array?
Angius
Angius2y ago
Some bitwise shenanigans
nut
nutOP2y ago
i can see that but i have no idea how it works
Angius
Angius2y ago
If MouseButtons is an enum, see if it has a [Flags] attribute Or perhaps some other, manual way of going about it
nut
nutOP2y ago
ohh itdoes
Angius
Angius2y ago
The principle is a bitmask, essentially
Angius
Angius2y ago
FlagsAttribute Class (System)
Indicates that an enumeration can be treated as a bit field; that is, a set of flags.
nut
nutOP2y ago
im converting this from winforms to wpf and MouseButton didnt work at all with it, ill just use a list instead of bitwise shenanigans lol ty
Want results from more Discord servers?
Add your server