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 except programming questions.

Technical discussions are encouraged, but click here to ask your programming question.

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, no programming questions and please don't post ads.


 
GeneralRe: Job Interview completed. PinmemberSoMad20-Mar-13 20:17 
GeneralRe: Job Interview completed. PinprotectorAspDotNetDev20-Mar-13 20:29 
GeneralRe: Job Interview completed. PinmemberTadit Dash20-Mar-13 21:13 
GeneralRe: Job Interview completed. PinmemberEdo Tzumer20-Mar-13 22:57 
GeneralRe: Job Interview completed. PinmemberPHS24121-Mar-13 0:02 
GeneralRe: Job Interview completed. PinprotectorPete O'Hanlon21-Mar-13 2:15 
NewsThis explains that last meeting I was in PinmemberKent Sharkey20-Mar-13 16:33 
GeneralRe: This explains that last meeting I was in PinmemberThePhantomUpvoter20-Mar-13 17:38 
GeneralRe: This explains that last meeting I was in Pinmemberwizardzz20-Mar-13 18:11 
GeneralRe: This explains that last meeting I was in PinmentorPaul Watt20-Mar-13 19:02 
GeneralRe: This explains that last meeting I was in Pinmemberlewax0020-Mar-13 19:43 
GeneralRe: This explains that last meeting I was in Pinmember Michael Martin 21-Mar-13 0:05 
GeneralRe: This explains that last meeting I was in PinmemberMark_Wallace21-Mar-13 0:22 
GeneralDoes this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 13: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 14:43 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 15:00 
GeneralRe: Does this language feature seem strange to anyone else? PinprotectorAspDotNetDev20-Mar-13 15:04 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 15:11 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberPIEBALDconsult20-Mar-13 15:17 
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? Pinmemberlewax0020-Mar-13 15:13 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberMatthew Faithfull20-Mar-13 15:29 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 15:39 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberMatthew Faithfull20-Mar-13 16:10 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberMike Winiberg21-Mar-13 23:14 
GeneralRe: Does this language feature seem strange to anyone else? PinprotectorAspDotNetDev20-Mar-13 14:56 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0020-Mar-13 15:06 
GeneralRe: Does this language feature seem strange to anyone else? PinprotectorAspDotNetDev20-Mar-13 15:09 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberjschell21-Mar-13 11:04 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0021-Mar-13 11:27 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberCésar de Souza22-Mar-13 1:43 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 11:58 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberenglebart22-Mar-13 3:26 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 11:41 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberRafagaX22-Mar-13 7:40 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberChadwick Posey22-Mar-13 8:48 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 11:45 
GeneralReading Code PinmemberKyudos20-Mar-13 12:22 
GeneralRe: Reading Code [modified] Pinmemberwizardzz20-Mar-13 12:29 
GeneralRe: Reading Code PinprotectorAspDotNetDev20-Mar-13 12:40 
JokeRe: Reading Code PinmemberKyudos20-Mar-13 12:47 
GeneralRe: Reading Code Pinmemberlewax0020-Mar-13 12:42 
GeneralRe: Reading Code PinmemberCDP180220-Mar-13 12:39 
GeneralRe: Reading Code Pinmemberdusty_dex20-Mar-13 12:45 
JokeRe: Reading Code PinmemberKyudos20-Mar-13 12:48 
GeneralRe: Reading Code PinmemberPIEBALDconsult20-Mar-13 13:50 
GeneralRe: Reading Code PinmemberRob Grainger22-Mar-13 4:00 
GeneralRe: Reading Code PinmvpEddy Vluggen20-Mar-13 13:45 
GeneralRe: Reading Code Pinmember_Josh_20-Mar-13 14:31 
GeneralRe: Reading Code PinmemberPIEBALDconsult20-Mar-13 14:35 

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.150414.1 | Last Updated 21 Apr 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid