8

As a follow on from this question.

How can I call a function and pass in an Enum?

For example I have the following code:

enum e1
{
    //...
}

public void test()
{
    myFunc( e1 );
}

public void myFunc( Enum e )
{
    var names = Enum.GetNames(e.GetType());

    foreach (var name in names)
    {
        // do something!
    }

}

Although when I do this I am getting the 'e1' is a 'type' but is used like a 'variable' Error message. Any ideas to help?

I am trying to keep the function generic to work on any Enum not just a specific type? Is this even possible?... How about using a generic function? would this work?

Community
  • 1
  • 1
TK.
  • 44,657
  • 46
  • 115
  • 146
  • @TK, see my recent edit for typesafe. – bruno conde Feb 03 '09 at 15:27
  • I'd like an explanation _why_ you want to do this. To me it looks like you try to find a solution for a problem that wouldn't exist if you'd rethink your aproach from a few steps before so an explanation might help finding a solution for the real problem. – Morfildur Feb 15 '10 at 16:33

4 Answers4

9

Why not passing the type? like:

 myfunc(typeof(e1));

public void myFunc( Type t )
{
}
Martin Moser
  • 6,049
  • 1
  • 25
  • 39
  • if I use "if ( e is Enum )" or the 'as' statement, then I get an error "Cannot convert type 'System.Type' to 'System.Enum' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion" – TK. Feb 03 '09 at 15:08
  • You can still use Enum.GetNames(t) – Jon B Feb 03 '09 at 15:12
  • How could I type-safe this to ensure that ony enums are used instead of any type? – TK. Feb 03 '09 at 15:18
  • Use typeof(Enum).IsAssignableFrom(t) - that will return false of t does not represent an Enum type. – Andy Feb 03 '09 at 15:30
9

You can use a generic function:

    public void myFunc<T>()
    {
        var names = Enum.GetNames(typeof(T));

        foreach (var name in names)
        {
            // do something!
        }
    }

and call like:

    myFunc<e1>();

(EDIT)

The compiler complains if you try to constraint T to Enum or enum.

So, to ensure type safety, you can change your function to:

    public static void myFunc<T>()
    {
        Type t = typeof(T);
        if (!t.IsEnum)
            throw new InvalidOperationException("Type is not Enum");

        var names = Enum.GetNames(t);
        foreach (var name in names)
        {
            // do something!
        }
    }
bruno conde
  • 47,131
  • 14
  • 97
  • 117
5

You are trying to pass the type of the enum as an instance of that type - try something like this:

enum e1
{
    foo, bar
}

public void test()
{
    myFunc(e1.foo); // this needs to be e1.foo or e1.bar - not e1 itself
}

public void myFunc(Enum e)
{
    foreach (string item in Enum.GetNames(e.GetType()))
    {
        // Print values
    }
}
Jon B
  • 49,709
  • 30
  • 129
  • 160
Andrew Hare
  • 333,516
  • 69
  • 632
  • 626
  • I am aware that I canpass individual values like this. I am looking to pass a generic Enum to a function to say print all the values not just one. – TK. Feb 03 '09 at 15:03
  • This will print all the values. Use Enum.GetNames(e.GetType()). – Jon B Feb 03 '09 at 15:10
0

Use

public void myFunc( e1 e ) { // use enum of type e}

instead of

public void myFunc( Enum e ) { // use type enum. The same as class or interface. This is not generic! }
abatishchev
  • 95,331
  • 80
  • 293
  • 426
  • I was trying to keep the function generic to work on any Enum not just a specific type? – TK. Feb 03 '09 at 15:09