473,473 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing enums to a method

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
5 10055
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
7
by: chukkykzn | last post by:
Hi I'd like to pass an entire enumeration to a method. Not sure how it's done. Eg: enum MyEnum { one = 1, two = 2 } class Test
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
1
by: Powers | last post by:
I currently have a large number of enums implemented into my Web Service. I am reworking the XML serialization of these enums. At present, each enum has hardcoded values, for example: xAttrs...
10
by: Jay | last post by:
In C# I can set up an Enum so that number are represented as keywords, which is very useful. Is there such a datatype in a database? I suppose I could use an extra table, with the ID column as...
4
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.