473,835 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing enum wholly

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
{

public Test() {MethodA();}
void MethodA()
{
MethodB(/*send in MyEnum*/);
}

void MethodB(/*Take in MyEnum*/)
{
/*do some stuff with myEnum*/
}

}

Nov 17 '05 #1
7 1595
chukkykzn,

Why not pass the type of the enumeration?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"chukkykzn" <ch*******@webm ail.co.za> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
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
{

public Test() {MethodA();}
void MethodA()
{
MethodB(/*send in MyEnum*/);
}

void MethodB(/*Take in MyEnum*/)
{
/*do some stuff with myEnum*/
}

}

Nov 17 '05 #2
I don't think you can pass enum directly. Insted of this try to send int
values in enum.

I hope this will help
Kiran

"chukkykzn" wrote:
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
{

public Test() {MethodA();}
void MethodA()
{
MethodB(/*send in MyEnum*/);
}

void MethodB(/*Take in MyEnum*/)
{
/*do some stuff with myEnum*/
}

}

Nov 17 '05 #3

chukkykzn wrote:
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
{

public Test() {MethodA();}
void MethodA()
{
MethodB(/*send in MyEnum*/);
}

void MethodB(/*Take in MyEnum*/)
{
/*do some stuff with myEnum*/
}

}


If MethodB is designed to do 'some stuff' with an arbitrary enum, it
should accept a parameter of type System.Enum (since all enums are
derived from System.Enum)

--
Larry Lard
Replies to group please

Nov 17 '05 #4
Larry Lard <la*******@hotm ail.com> wrote:
If MethodB is designed to do 'some stuff' with an arbitrary enum, it
should accept a parameter of type System.Enum (since all enums are
derived from System.Enum)


No, that would be a specific enum *value*.

Better would be to take a parameter of type System.Type, where the type
passed has to derive from System.Enum.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

Jon wrote:
Larry Lard <la*******@hotm ail.com> wrote:
If MethodB is designed to do 'some stuff' with an arbitrary enum, it
should accept a parameter of type System.Enum (since all enums are
derived from System.Enum)


No, that would be a specific enum *value*.

Better would be to take a parameter of type System.Type, where the type
passed has to derive from System.Enum.


Hmm I think that's what I meant to say. But...

Is there a way to specify that restriction on a parameter at compile
time?

--
Larry Lard
Replies to group please

Nov 17 '05 #6
Larry Lard <la*******@hotm ail.com> wrote:
No, that would be a specific enum *value*.

Better would be to take a parameter of type System.Type, where the type
passed has to derive from System.Enum.


Hmm I think that's what I meant to say. But...

Is there a way to specify that restriction on a parameter at compile
time?


Unfortunately not, as far as I know.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Jon wrote:
Larry Lard <la*******@hotm ail.com> wrote:
Is there a way to specify that restriction on a parameter at compile
time?


Unfortunately not, as far as I know.


Could you do that with generics? Like this: (not sure if this will
work)

public static void EnumOperation<T >(T t) where T : system.Enum
{
//Manipulate t here
}

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4859
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
6
16151
by: | last post by:
I have MyEnumeratedSet { ... some values go here ... } I have a variable that is defined as MyEnumeratedSet MyVariable; I have a function that needs to be able to set a variable by Ref MyFunction(ref System.Enum Parameter) { ..... Parameter has to be set here } so I have tried calling it by MyFunction(ref (Enum) MyVariable); unfortunately this yields compiler errors and it seems like there is no way
16
82867
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
1
1622
by: Julia | last post by:
Hi, I am calling a remote object from ASP.NET application. when passing an ENUM to a method i am getting an exception "Server encountered an internal error. For more information, turn on customErrors in the server's .config file." The enum is valid and all other methdos are working fine. The enum is mark with
3
1925
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford www.IntraRELY.com
1
1789
by: martin | last post by:
This code compiles (this is how it should work): Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); This code does not compile (passing an int instead of an AddressFamily is bad): Socket sock = new Socket(1, SocketType.Stream, ProtocolType.Tcp); But why on earth does not compile: Socket sock = new Socket(0, SocketType.Stream, ProtocolType.Tcp);
1
5248
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
0
9653
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10815
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10524
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7768
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6968
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4434
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 we have to send another system
2
3997
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.