Passing enum wholly

C

chukkykzn

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*/
}

}
 
N

Nicholas Paldino [.NET/C# MVP]

chukkykzn,

Why not pass the type of the enumeration?

Hope this helps.
 
G

Guest

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
 
L

Larry Lard

chukkykzn said:
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)
 
J

Jon Skeet [C# MVP]

Larry Lard said:
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.
 
L

Larry Lard

Jon said:
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?
 
J

Jon Skeet [C# MVP]

Larry Lard said:
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.
 
C

Chris Dunaway

Jon said:
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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top