PC Review


Reply
Thread Tools Rate Thread

Casting as an enum type

 
 
NullQwerty
Guest
Posts: n/a
 
      30th Apr 2007
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!

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      30th Apr 2007
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 Removed)
"NullQwerty" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
>



 
Reply With Quote
 
NullQwerty
Guest
Posts: n/a
 
      30th Apr 2007
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]
> - (E-Mail Removed)
> "NullQwerty" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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!
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting a string to an enum kc Microsoft VB .NET 8 23rd Jul 2005 07:19 PM
Q: Why casting an enum? Visual Systems AB \(Martin Arvidsson\) Microsoft C# .NET 15 13th Jun 2005 02:39 AM
Casting of Enum values Jason Larion Microsoft VB .NET 6 31st Jan 2005 04:50 PM
what logic behind casting enum =?Utf-8?B?RmVpIExp?= Microsoft C# .NET 2 18th Nov 2004 03:01 PM
Casting a value from a table to an Enum using Enum.ToObject KyleK Microsoft C# .NET 1 11th May 2004 04:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.