473,582 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ 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(t ypeof(myEnum));
}

void CallingMethod() {
UseEnum(Test1);
UseEnum(Test2);
}
Jan 27 '07 #1
5 10087
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(t ypeof(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(t ypeof(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(t ypeof(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******** ******@TK2MSFTN GP06.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(t ypeof(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******** ******@TK2MSFTN GP06.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(t ypeof(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
36390
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 variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
2
2065
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 seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the...
20
4820
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 EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
16
82777
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
1587
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
1986
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 seem to find an appropriate Reflection method to access Enums within a class Thanks! --- Posted using Wimdows.net Newsgroups -...
1
5230
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 = New XmlAttributes xEnum = New XmlEnumAttribute xEnum.Name = "0" xAttrs.XmlEnum = xEnum
10
3837
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 the number, and a corresponding column with strings representing the keywords. This sounds a bit inefficient - is there a better way?
4
2990
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 "properly" yet "ugly" is to test the state for each state. I know that enumes are not ordered but since they are "stored" as numbers they have an inherent...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8183
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6569
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5685
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.