Consider the following Enum:
Enum AnimalType
{
Dog = 1,
Cat = 2,
Bird = 4,
Wolf = 8
}
Now suppose we want to find all possible flag combinations where Dog is active, for example.
I divised the following method to do this:
public static int[] testss(int value)
{
var animalTypes = (AnimalType)value;
List<int> possibleValues = new List<int>();
possibleValues.Add(value);
int totalEnumValues = Enum.GetNames(typeof(AnimalType)).Length;
List<int> helper = new List<int>();
int cnt = 0;
for (int i = 1; i < totalEnumValues; i++)
{
foreach (Enum val in Enum.GetValues(animalTypes.GetType()))
{
if (cnt >= i)
break;
if (i == 1)
{
if ((AnimalType)val != (AnimalType)value)
{
possibleValues.Add((int)(AnimalType)val + value);
}
}
else
{
if ((AnimalType)val != (AnimalType)value && (cnt < i))
{
helper.Add((int)(AnimalType)val);
cnt += 1;
}
}
}
if (cnt > 0)
{
possibleValues.Add(helper.Sum() + value);
helper = new List<int>();
cnt = 0;
}
}
return possibleValues.ToArray();
}
This method will build an array with all the possible numeric representations containing a given flag as input.
It works only partially, if you test it for Dog (1) for example, you'll see that 2 values are missing from the possibleValues array.
Can you help me realizing where I went wrong?
You could use the
[Flags]
enum attribute, then an extension method. Something like this:It spins through all possible integer values of the enum, and sees if the 'flag' for the supplied animal is present. If it is, it adds to the return array.