Click here to Skip to main content

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy.
The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct and please don't post ads.

Technical discussions are welcome, but if you need specific help please use the programming forums.


 
GeneralDoes this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 12:58 
Part of an application I work on involves navigation through a series of documents, previously the navigate function looked like:
void NavigateTo(int document)
but as part of a redesign that made a single numeric index lose it's meaning, I changed it to
void NavigateTo(NavigationDirection direction)
where NavigationDirection is an enum consisting of Forward and Backward. As expected, the change caused some compilation errors where it was being called, because C# doesn't allow implicit conversion of int to enum. Having written the section of code it was in, I was pretty sure I knew where all the calls were, and the compiler seemed to agree...but the navigation was acting funny, it started at the second document. Debugged it, and found a third call to the function, that looked like this:
NavigateTo(0);
and 0 was the value of Forward, which was then moving it to the 2nd document, added a third value to the enum, None, since the first document was already being loaded in a new way, and this was just moving past it (the navigation has some side effects, loading other things related to the document, so the call was still needed...it makes more sense in the actual code, I'm just simplifying it). But hold on, I thought there wasn't an implicit conversion from int to enum types in C#?
 
So I played around with it a bit, and here's what I discovered: ints are not implicitly convertible to enum types (as I thought)...but const ints are 0 is! Both integer literals, and const int variables, as long as the value maps to a value in the enum (if it had been a number greater than 1 other than 0, I would have had the expected compiler error). While I understand how the compiler can implement this, I don't understand why they would, it seems to go against the C# language itself. I don't think you could even write your own implicit conversion that behaves the same way, since from what I can tell C# doesn't generally make a distinction between const and non-const versions of the same type for the purposes of parameters (e.g. int and const int can be used interchangeably), with the exception of ref and out...but that causes restriction in the reverse direction (only non-const).
 
Seems like an odd language design choice to me D'Oh! | :doh:
 
EDIT: looks like I jumped the gun a little on this one, apparently, it only works for 0. Which is probably worse...
GeneralRe: Does this language feature seem strange to anyone else? PinmemberPIEBALDconsult20-Mar-13 13:43 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 14:00 
GeneralRe: Does this language feature seem strange to anyone else? PinprotectorAspDotNetDev20-Mar-13 14:04 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 14:11 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberPIEBALDconsult20-Mar-13 14:17 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberMatthew Faithfull20-Mar-13 14:10 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 14:13 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberMatthew Faithfull20-Mar-13 14:29 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 14:39 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberMatthew Faithfull20-Mar-13 15:10 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberMike Winiberg21-Mar-13 22:14 
GeneralRe: Does this language feature seem strange to anyone else? PinprotectorAspDotNetDev20-Mar-13 13:56 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 14:06 
GeneralRe: Does this language feature seem strange to anyone else? PinprotectorAspDotNetDev20-Mar-13 14:09 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberjschell21-Mar-13 10:04 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0021-Mar-13 10:27 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberCésar de Souza22-Mar-13 0:43 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 10:58 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberenglebart22-Mar-13 2:26 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 10:41 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberRafagaX22-Mar-13 6:40 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberChadwick Posey22-Mar-13 7:48 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 10:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 29 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid