Passing enums to a method

J

Jay

I want to be able to pass different enums into a method which makes use of the enum in some way.
I've put together a simplified example below. How do I correctly pass an enum (eg Test1 or Test2 as
shown in CallingMethod) to the UseEnum method?

enum Test1 {Err, Start, Block, Hold}
enum Test2 {Err, Book, Plug}

void UseEnum(Enum myEnum){
string[] enumStr = Enum.GetNames(typeof(myEnum));
}

void CallingMethod(){
UseEnum(Test1);
UseEnum(Test2);
}
 
P

pigeonrandle

Hi,
I think you need to pass the enum as an object?
enum Test1 {Err, Start, Block, Hold}
enum Test2 {Err, Book, Plug}

void UseEnum(Object myEnum){
string[] enumStr = Enum.GetNames(typeof(myEnum));

}void CallingMethod(){
UseEnum(Test1);
UseEnum(Test2);

but i'm not sure. You should be able to use reflection on the object.

HTH,
James.

I want to be able to pass different enums into a method which makes use of the enum in some way.
I've put together a simplified example below. How do I correctly pass an enum (eg Test1 or Test2 as
shown in CallingMethod) to the UseEnum method?

enum Test1 {Err, Start, Block, Hold}
enum Test2 {Err, Book, Plug}

void UseEnum(Enum myEnum){
string[] enumStr = Enum.GetNames(typeof(myEnum));

}void CallingMethod(){
UseEnum(Test1);
UseEnum(Test2);



}- Hide quoted text -- Show quoted text -
 
L

Laurent Bugnion [MVP]

Hi,
I want to be able to pass different enums into a method which makes use of the enum in some way.
I've put together a simplified example below. How do I correctly pass an enum (eg Test1 or Test2 as
shown in CallingMethod) to the UseEnum method?

enum Test1 {Err, Start, Block, Hold}
enum Test2 {Err, Book, Plug}

void UseEnum(Enum myEnum){
string[] enumStr = Enum.GetNames(typeof(myEnum));
}

void CallingMethod(){
UseEnum(Test1);
UseEnum(Test2);
}

You cannot pass a Type (Test1) as parameter of a method like this. You
can, however, pass it like this:

void UseEnum( Type myEnumType )
{
string[] enumStr = Enum.GetNames( myEnumType );
}

and then

UseEnum1( typeof( E1 ) );


HTH,
Laurent
 
S

Stoitcho Goutsev \(100\)

Jay,

Looking at your code you want to pass a Type object for the enums. In this
case you can use *typeof* c# operator the get Type object from a class name

UseEnum(typeof(Text1);
UseEnum(typeof(Text2);
 
L

Laurent Bugnion [MVP]

Hi,
Jay,

Looking at your code you want to pass a Type object for the enums. In this
case you can use *typeof* c# operator the get Type object from a class name

UseEnum(typeof(Text1);
UseEnum(typeof(Text2);

Quite precisely what I said, except that you missed the closing
parenthesis ;-)

UseEnum(typeof(Text1));
UseEnum(typeof(Text2));

HTH,
Laurent
 
J

Jay

Thanks everyone for your suggestions. I've updated the code and that part now compiles error-free.


"Jay" <-> wrote in message I want to be able to pass different enums into a method which makes use of the enum in some way.
I've put together a simplified example below. How do I correctly pass an enum (eg Test1 or Test2 as
shown in CallingMethod) to the UseEnum method?

enum Test1 {Err, Start, Block, Hold}
enum Test2 {Err, Book, Plug}

void UseEnum(Enum myEnum){
string[] enumStr = Enum.GetNames(typeof(myEnum));
}

void CallingMethod(){
UseEnum(Test1);
UseEnum(Test2);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top