1

I'm trying to create a method that counts the number of times a given Enum occurs within some existing dictionaries for reporting purposes:

private static Dictionary<string, int> CountEnumOccurrence(Dictionary<Enum, List<string>> valueSource)
{
    var convertedDictionary = new Dictionary<string, int>();

    foreach (var entry in valueSource)
    {
        var name = Enum.GetName(entry.Key.GetType(), entry.Key);
        convertedDictionary.Add(name, entry.Value.Count);
    }

    return convertedDictionary;
}

However, if I attempt to call this method like so:

var criticalFailureCounts = CountEnumOccurrence(reportSpan.criticalFailures));

I get

"cannot convert from 'System.Collections.Generic.Dictionary<Reporter.CriticalFailureCategory,System.Collections.Generic.List<string>>' to 'System.Collections.Generic.Dictionary<System.Enum,System.Collections.Generic.List<string>>'"

Even though Reporter.CriticalFailureCategory is an Enum. I'm obviously doing this the wrong way, but I feel like there should be some way to achieve it.

Here's the definition for Reporter.CriticalFailureCategory at present:

namespace Reporter
{
    [DataContract(Namespace = "")]
    public enum CriticalFailureCategory
    {
        [EnumMember]
        ExcessiveFailures,
        [EnumMember]
        StalledInConfiguration
    }
}

The idea is that this can be expanded indefinitely without having to rewrite the code that reports on it.

ZeeMox
  • 25
  • 5
  • can you show the actual definition / structure of the CriticalFailureCategory enum..? – MethodMan Mar 09 '15 at 18:32
  • can you show the actual definition / structure of the CriticalFailureCategory enum..? also your `Enum.GetName` is incorrect it should be something like this for example `var name = Enum.GetName(typeof(entry.Key)` or change the `foreach` to `foreach(var entry in Enum.GetValues(typeof(valueSource)))` – MethodMan Mar 09 '15 at 18:40
  • @MethodMan: Thanks. I didn't even reach the point where I could test that code since I couldn't call the function. I added the definition to the original post. – ZeeMox Mar 09 '15 at 18:42
  • @MethodMan That's incorrect `typeof` requires a type name, not a instance of that type. – juharr Mar 09 '15 at 18:48
  • @juharr I use typeof to for example return the names of an enum with zero issues perhaps I am not giving a clear example.. – MethodMan Mar 09 '15 at 18:50
  • @MethodMan The only use of `typeof` that I've seen work is the actual name of the type like `typeof(DateTime)`. AFAIK it doesn't work with a variable. You can use it on generic types though. – juharr Mar 09 '15 at 18:52
  • it works with an Enum I am looking at some existing code that I have just tested as well as what we use in our production currently if you are not familiar with other ways of doing it I would be happy to show you ...feel free to send me a private chat and I can show you exactly the how and why it works.. – MethodMan Mar 09 '15 at 18:54
  • @MethodMan If you mean you have an `enum MyEnum...` and you use `typeof(MyEnum)` then sure, but `var someEnum = MyEnum.AEnum` with `typeof(someEnum)` doesn't even compile. – juharr Mar 09 '15 at 18:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72603/discussion-between-methodman-and-juharr). – MethodMan Mar 09 '15 at 19:00

1 Answers1

3

You need to make CountEnumOccurrence generic for this to work.

private static Dictionary<string, int> CountEnumOccurrence<TEnum>(
    Dictionary<TEnum, List<string>> valueSource)
{
    var convertedDictionary = new Dictionary<string, int>();

    foreach (var entry in valueSource)
    {
        var name = Enum.GetName(entry.Key.GetType(), entry.Key);
        convertedDictionary.Add(name, entry.Value.Count);
    }

    return convertedDictionary;
}

If you want to constrain the TEnum type to only enums you can check out this question to see how and why it has to be a partially run time check or you have to write MSIL.

Community
  • 1
  • 1
juharr
  • 31,741
  • 4
  • 58
  • 93