Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a flag based on a boolean

Does any have a more elegant way of doing this?

[Flags]
public enum SomeFlaggedEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4
}


private SomeFlaggedEnum _myFlags;

public bool EnabledValue1
{
    set 
    {
        if (value)
        {
            _myFlags |= SomeFlaggedEnum.Value1;
        }
        else
        {
            _myFlags &= ~SomeFlaggedEnum.Value1;
        }
    }
} 

I know there is probably a simple answer and I'm just way over thinking it...

EDIT: The enum was incorrect as pointed out in one of the answers. This was only in this example and not in the actual code.

like image 709
Adam Driscoll Avatar asked Aug 05 '11 15:08

Adam Driscoll


1 Answers

I mean, you could do:

_myFlags = value ? myFlags | SomeFlaggedEnum.Value1 : myFlags & ~SomeFlaggedEnum.Value1;

But I think your solution is fine.

like image 57
i_am_jorf Avatar answered Oct 25 '22 15:10

i_am_jorf