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?

2
  • @TK, see my recent edit for typesafe. Commented Feb 3, 2009 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. Commented Feb 15, 2010 at 16:33

4 Answers 4

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!
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

9

Why not passing the type? like:

 myfunc(typeof(e1));

public void myFunc( Type t )
{
}

4 Comments

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"
You can still use Enum.GetNames(t)
How could I type-safe this to ensure that ony enums are used instead of any type?
Use typeof(Enum).IsAssignableFrom(t) - that will return false of t does not represent an Enum type.
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
    }
}

2 Comments

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.
This will print all the values. Use Enum.GetNames(e.GetType()).
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! }

1 Comment

I was trying to keep the function generic to work on any Enum not just a specific type?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.