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

implicit cast for enums

Hi,

is it possible to define an implicit user-defined type conversion
operator for enums?

I'd like to convert my enum to an int without an explicit cast, e.g.
something like this:

------
public enum MyEnum : int
{
One = 1, Two, Three
}

....

MyEnum e = MyEnum.Two;
int i = (int)e; // this works
int j = e; // is it possible to achieve this
------
Thanks for your help,
Martin Bischoff

Nov 15 '05 #1
1 18153
Martin,

It is not possible to do this because enumerations are sealed when they
are created by the compiler.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Martin Bischoff" <ni*****@gmx.net> wrote in message
news:bf************@ID-159841.news.uni-berlin.de...
Hi,

is it possible to define an implicit user-defined type conversion
operator for enums?

I'd like to convert my enum to an int without an explicit cast, e.g.
something like this:

------
public enum MyEnum : int
{
One = 1, Two, Three
}

...

MyEnum e = MyEnum.Two;
int i = (int)e; // this works
int j = e; // is it possible to achieve this
------
Thanks for your help,
Martin Bischoff

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: pnsteiner | last post by:
i want to use the << operator defined for ostream with an object that itself knows nothing of the operator, but is castable to ostream&. it seems that C++ doesn't do implicit casts before invoking...
1
by: Bratislav Jevtic | last post by:
hi, Let's suppose I've got 2 classes: MyClass and MyClassColl (coll is inherited from ArrayList, for instance). But, when I invoke GetXXX or something returning object, I receive an object - not...
4
by: FB's .NET Dev PC | last post by:
Interesting note, the code below as is will attempt to cast what is clearly indicated as a string into a double. This is becuase the use of + as a concatenation operator. The error message...
10
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
1
by: not_a_commie | last post by:
I have an Angle class that I store angles in. It's basically just a bunch of fancy functions for manipulating a double. It has implicit casts for converting to/from double. Due to the project...
17
by: arnuld | last post by:
I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html FAQ discusses a special case when programmer has forgotten to do #include <stdlib.h>. I am including this header and I am not...
0
by: Kozy | last post by:
Greetings, i want to turn off implicit casting. The thing is we are writing db2 procedures on 9.7 version and when we try to build them on older production versions ( 9.5, 9.1 ) we have a lot of...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.