views:

2435

answers:

5

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?

+3  A: 

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
    }
}
Andrew Hare
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
This will print all the values. Use Enum.GetNames(e.GetType()).
Jon B
+6  A: 

Why not passing the type? like:

 myfunc(typeof(e1));

public void myFunc( Type t )
{
}
Martin Moser
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
You can still use Enum.GetNames(t)
Jon B
How could I type-safe this to ensure that ony enums are used instead of any type?
TK
Use typeof(Enum).IsAssignableFrom(t) - that will return false of t does not represent an Enum type.
Andy
A: 

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
I was trying to keep the function generic to work on any Enum not just a specific type?
TK
+5  A: 

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
A: 

bruno's version is really nice.

that's the one I will use

by the way. is there a way to make a stackoverflow question as a "like" or something? (save it under my user's activity for example, for later re-use - add-as-favourite-kinda thing)

Swoosh
You see the little star below the voting buttons. If you click it and then view your profile (click on your name at the top of the page). There is a 'Favourites' tab where it will display all the questions tagged as your 'favourite'
TK