472,958 Members | 1,731 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Casting of Nullable Enums

enum E = {a = 10, b = 10, c = 10};

object Value = 10;

E? En = (E?)(int?)Value;

Why do I need to caste it to (int?) before (E?)

On the other hand, if it is not nullable, it doesn't request to cast
to (int)

E En = (E) Value;

Thanks in advance.
Sep 1 '08 #1
1 8902
"CKKwan" <ck****@my-deja.comwrote in message
news:d2**********************************@v16g2000 prc.googlegroups.com...
enum E = {a = 10, b = 10, c = 10};

object Value = 10;

E? En = (E?)(int?)Value;

Why do I need to caste it to (int?) before (E?)
Because the boxed value is of type int, and not of your enum type. When
unboxing, you always have to unbox to the precise type of the value; any
conversions are not considered. Similarly, you can't box a float, and then
unbox it straight to an int, even though you can normally cast a float to a
nint.
On the other hand, if it is not nullable, it doesn't request to cast
to (int)

E En = (E) Value;
The fact that it works is actually incorrect, and it's more of a deficiency
in the existing .NET implementation than anything else. According to both
the language spec and the CLR spec, it shouldn't work. A similar case is
with a boxed int unboxable to uint and vice versa, even though they're two
distinct types.

Sep 1 '08 #2

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

Similar topics

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...
1
by: Fei Li | last post by:
Hi, Whi can help to expain what logic is when I cast enum a to b? Thanks
4
by: DKode | last post by:
I started using Enums to make my code more readable. Here is my ENUM: public enum EntryType : int { RegularHours = 1, Lunch = 2, Vacation = 3, Sick = 4, Personal = 5 }
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
10
by: Tyler Durden | last post by:
Hi, I have this annoying problem with a cast failing from a DAL that was created in 1.1 that I have included in a 2.0 app. Every stored proc in our system uses an integer parameter to return...
2
by: John Smith | last post by:
Hi All, (I first posted this question on the csharp.general but got no response, and since this NG is more active, so I repost it here) I have two enum definitions from two different...
4
by: PokerMan | last post by:
Hi guys, Maybe someone can explain thisi have this enum: public enum LimitType : int { BottomLimit, TopLimit, Limit }
1
by: =?Utf-8?B?QkpT?= | last post by:
I have written a Generic method that takes an object, a type, T, and returns a System.Nullable (Of T) depending on whether or not the object IsDBNull. It was working fine until I tried to pass a...
5
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have this snippet that don't compile: Math.Ceiling(-voceValue.Value - 0.5M); because the field Value of the class voceValue is decimal nullable. How can I solve? Thanks a lot.
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.