473,666 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18231
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************* *@exisconsultin g.com

"Martin Bischoff" <ni*****@gmx.ne t> 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
2032
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 a custom operator. i've tested with visual c++ 7 and the comeau online compilers, both required an explicit cast. is there a way to hint the compiler at using the cast operator implicitely, or is this not part of the language at all? (i have a...
1
1656
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 instance of MyClass. I've tried with implicit conversions - not working. Is there any way to inherit some collection that will have instances of custom class and make implicit conversion for return values? Practically, I need typed collection....
4
1791
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 generated is included with the code. Use & instead and it works. Dim pstrSend As String = "" 'ActionCode 1 and 2 are type BYTE, Msg is a byVal string parameter
10
2196
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 set a property on a class that takes an interface instance. I have a class that can cast implicit to a class that impliments said interface, but the compiler moans and says it cannot cast implicitly like that.
17
2497
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
1
3190
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 structure and where it stores the preferred SI units for display, I wanted to have the TypeConverter in a different project that has no reference to the angle class. However, returning a double from the ConvertFrom function throws an error that it...
17
2513
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 doing any explicit cast: #include <stdlib.h>
0
1393
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 problems because of implicit casting. Is there any parameter or a registry parameter that i can use to disable implicit casting? thank you for your answer, With regards, Marko
0
8783
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
8552
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,...
0
8640
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.