Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Enum to Array

Posted on 2009-07-16
Medium Priority
1,080 Views
Hello,

If I have an enum like this...

public enum Things : int
{
A = 1,
B = 2,
C = 4,
D = 8.
E = 16,
F = 32
}

How can I then convert an int value of 11 (for example) into a Thing[] array which would contain Thing.A, Thing.B and Thing.D (1+2+8 = 11)

Cheers.

ETFairFax
0
Question by:ETFairfax
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4

LVL 3

Expert Comment

ID: 24868014
I think you have to test for the bitflags and create/fill the array in a custom function.
Don't know a framework function which does this for you.

As bit test function I use they attached snippet..

``````        public static Boolean IsBitSet(Object bitField, Object checkBit)
{
return (((byte)(int)bitField & (byte)(int)checkBit) == (byte)(int)checkBit);
}
``````
0

LVL 39

Expert Comment

ID: 24868055
It is actually much simpler then what happel proposes. You can use the FlagsAttribute and simply cast the value. The following statement prints "A, B, D":

``````[Flags()]
private enum MyTestEnum
{
None = 0,
A = 1, B = 2, C = 4, D = 8, E = 16, F = 32
}

private void testEnum()
{
myTestEnum testEnum = (MyTestEnum) 11;
Debug.WriteLine(testEnum);
}
``````
0

LVL 39

Accepted Solution

abel earned 500 total points
ID: 24868215

1. You should assign the values as powers of 2
2. You should always assign "None = 0"
3. Each enum variable of Flags type can have multiple values (flags)
4. You can test flags with | (OR), & (AND) and ^ (XOR) as you would in any language
5. Same for assignment
6. You can use foreach or make it an array as follows:

``````// NOTE: this only works with the FlagsAttribute set on the enum!

// .NET 2.0 version:
private int[] EnumToArray(Enum someEnum)
{
List<int> list = new List<int>();
foreach (var it in Enum.GetValues(someEnum.GetType()))
{
if ((Convert.ToInt32(it) & Convert.ToInt32(someEnum)) != 0)
}
return list.ToArray();
}

// .NET 3.5 version:
private int[] EnumToArray(Enum someEnum)
{
return (from int en in Enum.GetValues(someEnum.GetType())
where (en & Convert.ToInt32(someEnum)) != 0
select en).ToArray();
}
``````
0

Author Closing Comment

ID: 31604158
Excellent.  Very informative.  Ta very much!!
0

LVL 39

Expert Comment

ID: 24868297
If you are lucky enough to be able to use .NET 3.5, consider creating an extension function for the Enum type, you can then use it for every Enum type in the same manner you are used to with other types that support Enumeration. Below is an example of a ToArray() extension function. You can call it like:

MyTestEnum someEnum = (MyTestEnum) 11;
int[] someArray = someEnum.ToArray();

This is of course the same idea, just a bit more convenient. ;-)

``````public static class EnumExtensions
{
public static int[] ToArray(this Enum someEnum)
{
return (from int en in Enum.GetValues(someEnum.GetType())
where (en & Convert.ToInt32(someEnum)) != 0
select en).ToArray();
}
}
``````
0

LVL 39

Expert Comment

ID: 24868301
Thanks for the follow up & the compliment!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldnâ€™t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldnâ€™t it be good if you could write code like this? (CODE) In .NET 3.5, this is possibleâ€¦
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct â€¦
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized â€¦
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increasedâ€¦
###### Suggested Courses
Course of the Month9 days, 12 hours left to enroll