Why is Enum.GetValues() not available in the Windows Phone 7 API, and does this mean I should generally shy away from Enums in favor of structs or other mechanisms.
2 Answers
I've run into this. For my purposes I was able to use reflection
foreach (var x in typeof(MyEnum).GetFields()) {
if (x.IsLiteral) {
// Do my stuff here
}
}
Really depends what you are doing with them though.
-
Neat solution! A similar solution is also discussed in ideas.dalezak.ca/2008/11/… and in an answer to stackoverflow.com/questions/105372/c-how-to-enumerate-an-enum– StuartMay 4, 2011 at 20:56
-
1Nice work around, partial credit (ie vote up) as had to give the answer credit to Stuart as his response addressed the question asked. but I should have asked how can I enumerate an enum, and you would have got the check.– CodeKiwiMay 5, 2011 at 5:18
-
2
MyEnum enumValue = (MyEnum)x.getValue(typeof(MyEnum));
will get you the value if required. Mar 21, 2012 at 21:44
Why is Enum.GetValues() not available in the Windows Phone 7 API
The "Why" is because WP7 is based on the "Compact Framework" - to save on resources, the compact framework does not contain every method in the full framework - and Enum.GetValues() was one of those omitted.
does this mean I should generally shy away from Enums in favor of structs or other mechanisms.
No - no particular reason. I'd recommend you continue to use enum's where you find them the most appropriate programming solution.