By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,931 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,931 IT Pros & Developers. It's quick & easy.

Casting as an enum type

P: n/a
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!

Apr 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com
"NullQwerty" <Nu********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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!

Apr 30 '07 #2

P: n/a
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!


Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mv*@spam.guard.caspershouse.com
"NullQwerty" <Nu********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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!
Apr 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.