473,385 Members | 1,757 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,385 software developers and data experts.

Casting as an enum type

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
2 2452
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h,...
7
by: Jean Stax | last post by:
Hi ! Jeffrey Richter gives the following example in his book: struct Point { Int32 x, y; } class AAA{ .... public virtual void Add(Object value);
3
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
8
by: kc | last post by:
I'm trying to pull data from a database that I have no control over the structure of. There's a members table with a column for the member's sex. It just stores the sex as M or F. I'd like to...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: mitdej | last post by:
Hi there, I have an several enum types that starts from a nunmber other than 0. For example: public enum InternalStatus { Pending = 1, Ported = 2, Suspended = 3 } I put this values in a int...
4
by: Rene | last post by:
If variance/covariance is not allowed in array of value types, why is the expression on the "Main" method run successfully (see snippet below)? I am missing something? Thank you. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.