views:

1431

answers:

6

The following returns

Type of conditional expression cannot be determined because there is no implicit conversion between 'double' and '<null>'

aNullableDouble = (double.TryParse(aString, out aDouble)?aDouble:null)


The reason why I can't just use aNullableBool instead of the roundtrip with aDouble is because aNullableDouble is a property of a generated EntityFramework class which cannot be used as an out par.

+1  A: 
aNullableDouble = (double.TryParse(aString, out aDouble)?new Nullable<double>(aDouble):null)
James Curran
new Nullabled()?! Isn't that what the double? syntax is for?!
kronoz
Yeah, that'll work too. Mine more explicit, but both forms will generate identical code.
James Curran
+6  A: 
aNullableDouble = double.TryParse(aString, out aDouble) ? (double?)aDouble : null;
kronoz
+6  A: 

Just blow the syntax out into the full syntax instead of the shorthand ... it'll be easier to read:

aNullableDouble = null;
if (double.TryParse(aString, out aDouble))
{
    aNullableDouble = aDouble;
}
Joel Martinez
Nice. That does add clarity.
kronoz
A: 

.NET supports nullable types, but by declaring them as such you have to treat them a bit differently (as, understandably, something which is normally a value type now is sort of reference-ish).

This also might not help much if you end up having to do too much converting between nullable doubles and regular doubles... as might easily be the case with an auto-generated set of classes.

Yadyn
A: 

Put me on a stick and call me charly :s
That, my kind sirs, was a stupid mistake of mine. I was looking why it wanted to be able to cast the true return and the false return. Whilst off course I should have been looking at the cast from both returns to my aNullableDouble :P

Calls for a coffee brake I guess.

borisCallens
A: 

The interesting side-effect of using nullable types is that you can't really use a shorthand IF. Shorthand IF has to return the same Type from both conditions, and it can't be null in either case. So, cast or write it out :)