Casting as an enum type

N

NullQwerty

Hey folks,

So, I've got three enum types:
enum enum1
enum enum2
enum enum3

And then I've got a function overloaded three times to accept each
enum type:
private void func(enum1 myenum){}
private void func(enum2 myenum){}
private void func(enum3 myenum){}

And then I've got a wrapper function (kind of) which accepts a
System.Enum as a parameter, and then passes that parameter to call the
functions above:
private void funcCaller(System.Enum enumType)
{
func(enumType);
}

Finally the wrapper function is called passing it one of the enum
types
funcCaller(enum1);
funcCaller(enum2);
funcCaller(enum3);

This fails however inside funcCaller with the error:
Argument '1': cannot convert from 'System.Enum' to 'enum1'

So, basically it seems it's not doing implicit casting. I know there
are other ways to accomplish what I need to do, but this is the way
I'd like most to do it, so before I move on, does anyone know how I
can get this to work in this manner? How can I get the line inside
funcCaller to work? Is it just a matter of using a typeof or
something? I don't want to use a switch statement in there because I
don't want to update it everytime a new enum is created.

This ic C# 1.1 by the way...so no generics!

Thanks a lot!
 
N

Nicholas Paldino [.NET/C# MVP]

You are going to have to do a check for each type in your function,
something along the lines of:

if (enumType.GetType() == typeof(enum1))
// Work with enum1.

if (enumTpe.GetType() == typeof(enum2))
// Work with enum2.

Hope this helps.
 
N

NullQwerty

Thanks. Yeah, I knew I could do that, but didn't want to because it
is in an abstract class and is called from the child classes that
implement the abstract class. New enums can be created a few times a
year, and I don't want the programmers that implement a new class to
have to change the parent class (or to know that they have to). I
just want them to implement the class (especially since some of them
could be coming straight out of school and messing up this class could
cause lots of damage :) ).

Thanks though, I figured this was the case!



You are going to have to do a check for each type in your function,
something along the lines of:

if (enumType.GetType() == typeof(enum1))
// Work with enum1.

if (enumTpe.GetType() == typeof(enum2))
// Work with enum2.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
NullQwerty said:
Hey folks,

So, I've got three enum types:
enum enum1
enum enum2
enum enum3

And then I've got a function overloaded three times to accept each
enum type:
private void func(enum1 myenum){}
private void func(enum2 myenum){}
private void func(enum3 myenum){}

And then I've got a wrapper function (kind of) which accepts a
System.Enum as a parameter, and then passes that parameter to call the
functions above:
private void funcCaller(System.Enum enumType)
{
func(enumType);
}

Finally the wrapper function is called passing it one of the enum
types
funcCaller(enum1);
funcCaller(enum2);
funcCaller(enum3);

This fails however inside funcCaller with the error:
Argument '1': cannot convert from 'System.Enum' to 'enum1'

So, basically it seems it's not doing implicit casting. I know there
are other ways to accomplish what I need to do, but this is the way
I'd like most to do it, so before I move on, does anyone know how I
can get this to work in this manner? How can I get the line inside
funcCaller to work? Is it just a matter of using a typeof or
something? I don't want to use a switch statement in there because I
don't want to update it everytime a new enum is created.

This ic C# 1.1 by the way...so no generics!

Thanks a lot!
 

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