Changing a flag based on a boolean
Asked Answered
P

5

7

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.

Porpoise answered 5/8, 2011 at 15:12 Comment(4)
Take a look at this #5851373Phosphor
what about a ternary operator instead of setting property?Rustice
@Adam: I got a discussion with guys arround what is "elegant" code here on SO. The elegant code is the code that is easy to read and understand. So I think you have already an elegant solution.Stunk
True, but if you have a lot of these things, a more compact representation may be more desirable. But you can always just #region them and compact them in the IDE, so, yeah, whatever.Cockeyed
C
8

I mean, you could do:

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

But I think your solution is fine.

Cockeyed answered 5/8, 2011 at 15:15 Comment(0)
Q
6

Well, you could use a conditional:

_myFlags = value ? _myFlags | SomeFlaggedEnum.Value1
                 : _myFlags & ~SomeFlaggedEnum.Value1;
Quaternion answered 5/8, 2011 at 15:14 Comment(0)
S
3

It looks like it'd get pretty ugly if you had a property per flag for setting it, assuming you'll have more than 3 flags. Would it not be easier to go for a single method where you can say which flags to set?

public void EnableFlag(SomeFlaggedEnum value, bool set) {
    _myFlags = set ? value | _myFlags : ~value & _myFlags;
}

EnableFlag(SomeFlaggedEnum.Value1, true);
Sikh answered 5/8, 2011 at 15:31 Comment(0)
H
3

Make a generic setter for flags (there is already a generic "HasFlag" in the Enum class, but an extension like this will complement it.

public static class EnumExtensions
{
    public static T SetFlags<T>(this T value, T flags, bool on) where T : struct
    {    
        long lValue = Convert.ToInt64(value);
        long lFlag = Convert.ToInt64(flags);
        if (on)
        {
            lValue |= lFlag;
        }
        else
        {
            lValue &= (~lFlag);
        }
        return (T)Enum.ToObject(typeof(T), lValue);
    }
}

Once you have that, you can make properties for each flag (but that quickly gets messy), or simply delegate getting and setting flags to HasFlag/SetFlag

Hanyang answered 5/8, 2011 at 15:34 Comment(1)
This should be the solution.Williamson
F
2

Your setter is fine.

The issue is that using FlagsAttribute doesn't make the compiler choose useful values for using the enum members as flags. SomeFlaggedEnum.Value1 is zero, because it's the first declared value in the enumeration, and so it doesn't represent a bit flag at all.

Try

[Flags]
public enum SomeFlaggedEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4
}
Fracture answered 5/8, 2011 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.