473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1582
chukkykzn,

Why not pass the type of the enumeration?

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

"chukkykzn" <ch*******@webmail.co.za> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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*******@hotmail.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.com>
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*******@hotmail.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*******@hotmail.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.com>
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*******@hotmail.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
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...
6
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...
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
1
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...
3
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...
1
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
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...

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.