473,626 Members | 3,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Syst em.Enum enumType)
{
func(enumType);
}

Finally the wrapper function is called passing it one of the enum
types
funcCaller(enum 1);
funcCaller(enum 2);
funcCaller(enum 3);

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 2478
You are going to have to do a check for each type in your function,
something along the lines of:

if (enumType.GetTy pe() == typeof(enum1))
// Work with enum1.

if (enumTpe.GetTyp e() == typeof(enum2))
// Work with enum2.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"NullQwerty " <Nu********@gma il.comwrote in message
news:11******** **************@ y80g2000hsf.goo glegroups.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(Syst em.Enum enumType)
{
func(enumType);
}

Finally the wrapper function is called passing it one of the enum
types
funcCaller(enum 1);
funcCaller(enum 2);
funcCaller(enum 3);

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.GetTy pe() == typeof(enum1))
// Work with enum1.

if (enumTpe.GetTyp e() == typeof(enum2))
// Work with enum2.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"NullQwerty " <Nu********@gma il.comwrote in message
news:11******** **************@ y80g2000hsf.goo glegroups.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(Syst em.Enum enumType)
{
func(enumType);
}

Finally the wrapper function is called passing it one of the enum
types
funcCaller(enum 1);
funcCaller(enum 2);
funcCaller(enum 3);

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
7019
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, which is included in file A.c (this is getting really confusing, I know!).I want to access the enum in file A.c, but don't want to include file C.h, for various reasons.I've tried casting the enum, but still get the error "Dereferencing pointer...
7
1419
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
2997
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 behind this construct was to enforce type safety and
18
11340
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
281
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 field UserType) and for each type of user a inherited class like SessionMasterUser and SessionNormalUser.
8
8609
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 create an enum to store the sex. Is there a way that I can create a method in the Enum to convert M to Male and F to Female, or does this method have to exist in another class? Thanks for any help!
3
3642
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' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
4
1979
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 column of a MSSQL table.
4
1438
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. --------------------- class Program { enum Foo
0
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.