1

Possible Duplicate:
Iterating through an enumeration in Silverlight?

I came up with a handy little function that gets me the count of an enum (I know it won't work properly with all enums). Rather than hard coding the Enum into the function such that I have to write a seprate function for each Enum I want to use it with, I wanted to pass the enum in as an argument but I am having difficulty figuring out how to do this.

Here is the code:

    private enum MyColors { Red, Green, Blue }

    private Int32 GetEnumCount()
    {
        Int32 i = 0;
        while (Enum.IsDefined(typeof(MyColors), (MyColors)i))
        {
            i++;
        }
        return i;
    }

UPDATE

I came up with this as the answer in the end:

    private Int32 GetEnumCount(Type enumType)
    {
        Int32 i = 0;
        while (Enum.IsDefined(enumType, i))
        {
            i++;
        }
        return i;
    }
Community
  • 1
  • 1
descf
  • 1,302
  • 1
  • 12
  • 31
  • Btw, the 2nd-highest answer on that page is the one I recommend. – Noldorin Oct 02 '11 at 16:10
  • This approach would only work where the enum values are contiguous. A `Flags` attributed enum would typically have gaps in the integers used so this approach would fail. Perhaps you could explain why you need this? – AnthonyWJones Oct 02 '11 at 16:15

3 Answers3

2

As an alternative (although clearly this has already been answered) if you start the first one with 0 then add one on at the end called count then you can use that, eg:

enum MyColour
{
     Blue = 0,
     Red,
     Green,
     ColourCount
}
T. Kiley
  • 2,752
  • 21
  • 30
1

This can be done, but there's some additional work needed to make it work with unusal enums:

private static ulong GetEnumContiguousCount(Type enumType)
{
    var underlyingType = Enum.GetUnderlyingType(enumType);
    ulong i;
    for (i = 0; Enum.IsDefined(enumType, Convert.ChangeType(i, underlyingType, null)); ++i) {}
    return i;
}

Demo: http://ideone.com/Serji

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • +1: The need to cope with a different underlying types makes sense, but returning `ulong` doesn't, `int` would do and would be easier to use. – AnthonyWJones Oct 02 '11 at 19:18
  • @AnthonyWJones: You're probably right, but I didn't both to look up the maximum number of members of an enum. – Ben Voigt Oct 02 '11 at 19:23
0

You should take the enum Type as an argument, and remove the cast to (MyColors).

In non-Sliverlight, you can also just replace your function with

Enum.GetValues(typeof(MyEnum)).Length
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    -1 for copying my answer in your edit! – Noldorin Oct 02 '11 at 16:02
  • @Noldorin: I wrote this answer before you did, and I wrote the edit before I saw your answer. – SLaks Oct 02 '11 at 16:03
  • No, you did not When I posted my answer, your `Enum.GetValues` bit was *not* included. I refreshed 30 seconds later and it was. You even called your enum type exactly the same thing... – Noldorin Oct 02 '11 at 16:04
  • I wrote the edit without refreshing the page; I hadn't seen your answer. `MyEnum` is an obvious name; note my lack of `var`. – SLaks Oct 02 '11 at 16:04
  • 3
    Notice the Silverlight tag. There is not getValues in Silverlight for Enums. – descf Oct 02 '11 at 16:05
  • 1
    -9999 You both copied the docs on Enum.GetValues(). For Shame!!! – Ritch Melton Oct 02 '11 at 17:13