Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When you use flag(Enum) you have a limit of 64. What are the alternative when you reach the limit?

Tags:

.net

With enum under .net the biggest number you can use is ULong.
This mean a maximum of 64 flags.

What would be the alternative when you need more than 64 flags?

Edit

Sorry, I forgot to add this, alternative that would still work with bitwise operations at least these one; and and or.

using Josh Einstein suggestion, I came up with this, does it make sense?

class bitArrayFlag
{
    private const int flagSize = 255; //allow X numbers of flags

    public BitArray flag1;
    public BitArray flag2;
    public BitArray flagN;

    public bitArrayFlag()
    {
        int flagPos = 0;
        bool[] flagBit = new bool[flagSize];

        flagBit[flagPos] = true;
        flag1 = new BitArray(flagBit);

        flagBit[flagPos] = false;
        flagPos += 1;
        flagBit[flagPos] = true;
        flag2 = new BitArray(flagBit);

        //...
        //...
        //...

        flagBit[flagPos] = false;
        flagPos += 1;
        flagBit[flagPos] = true;
        flagN = new BitArray(flagBit);
    }
}
like image 249
Fredou Avatar asked Mar 03 '10 14:03

Fredou


People also ask

What are the first 4 numeric values of an enum used to store flags?

We use the values 0, 1, 2, 4 to indicate the underlying bits for each value—we should double each value to avoid conflicts. Operators We use bitwise operators, like OR and AND, with enum flags. We use "NOT" to remove a flag from an enum.

What is the role of Flag attribute in enums?

The [Flag] attribute is used when Enum represents a collection of multiple possible values rather than a single value. All the possible combination of values will come. The [Flags] attribute should be used whenever the enumerable represents a collection of possible values, rather than a single value.

Why should you apply the flags attribute to an enum type when you want to store combined values?

Flags allow you to use bitmasking inside your enumeration. This allows you to combine enumeration values, while retaining which ones are specified.

What is Flag in VB net?

In general, "Flag" is just another term for a true/false condition. It may have more specific meanings in more specific contexts. For instance, a CPU may keep "arithmetic flags", each one indicating a true/false condition resulting from the previous arithmetic operation.


1 Answers

That many flags seems excessive and would suggest a redesign is needed. However, you could consider using two sets of flags. The first to designate the "flag group" and the second to designate the flags within that group. You'd have to have a class that then managed your "grouped enum" so that you could test if a flag was set or not in a simple way.

struct BigFlags<TGroupEnum, TFlagEnum>
{
    private Dictionary<TGroupEnum, TFlagEnum> flags;

    public BigFlags(IDictionary<TGroupEnum, TFlagEnum> flags)
    {
        this.flags = new Dictionary<TGroupEnum, TFlagEnum>(flags);
    }

    public BigFlags(TGroupEnum group, TFlagEnum flags)
    {
        this.flags = new Dictionary<TGroupEnum, TFlagEnum>() { { group, flags } };
    }

    public bool Contains(BigFlags<TGroupEnum, TFlagEnum> flags)
    {
        // TODO: Compare dictionaries and see if the passed flags are a subset of these flags.
    }

    // TODO: Equality to check exact match
    // TODO: Logical operators and operators for setting/removing flags.
}
like image 68
Jeff Yates Avatar answered Oct 21 '22 09:10

Jeff Yates