10

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.

CodeKiwi
  • 739
  • 2
  • 9
  • 20

2 Answers2

15

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.

Chris Sainty
  • 9,086
  • 1
  • 26
  • 31
  • Neat solution! A similar solution is also discussed in http://ideas.dalezak.ca/2008/11/enumgetvalues-in-compact-framework.html and in an answer to http://stackoverflow.com/questions/105372/c-how-to-enumerate-an-enum – Stuart May 04 '11 at 20:56
  • 1
    Nice 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. – CodeKiwi May 05 '11 at 05:18
  • 2
    `MyEnum enumValue = (MyEnum)x.getValue(typeof(MyEnum));` will get you the value if required. – Daniel Ballinger Mar 21 '12 at 21:44
5

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.

Stuart
  • 66,722
  • 7
  • 114
  • 165