By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,981 Members | 1,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,981 IT Pros & Developers. It's quick & easy.

Passing enums to a method

P: n/a
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);
}
Jan 27 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

On 27 Jan, 11:45, "Jay" <-wrote:
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 -
Jan 27 '07 #2

P: n/a
Hi,

Jay wrote:
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
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 27 '07 #3

P: n/a
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);
--
HTH
Stoitcho Goutsev (100)

"Jay" <-wrote in message news:Oa**************@TK2MSFTNGP06.phx.gbl...
>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);
}


Jan 29 '07 #4

P: n/a
Hi,

Stoitcho Goutsev (100) wrote:
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
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 29 '07 #5

P: n/a
Jay
Thanks everyone for your suggestions. I've updated the code and that part now compiles error-free.
"Jay" <-wrote in message news:Oa**************@TK2MSFTNGP06.phx.gbl...
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);
}

Jan 29 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.