On Sep 25, 9:52*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:
I have an expression like this:
decimal? ValoreComplessivoNettoDelfondoValue = (totaleAttivitaValue.HasValue
|| totalePassivitaValue.HasValue) ? ((totaleAttivitaValue ?? 0) -
(totalePassivitaValue ?? 0)) : null;
where every variable is of type Decimal?.
But VS 2005 returns me the error "Impossibile implicit conversion between
decimal and null".
Where's the problem?
Well, that many conditionals and null-coalescing operators makes the
code very difficult to read for a start. I would break it up into
separate statements - I suspect you'll find the problem goes away then
anyway. I believe the issue is demonstrated by this rather simpler
code:
decimal? foo = true ? 5m : null;
The types of the rightmost two operands are "decimal" and "<null>" and
there's no implicit conversion from either one to the other. If you
cast either of the operands to "decimal?" it will work fine:
decimal? foo = true ? (decimal?) 5m : null;
or
decimal? foo = true ? 5m : (decimal?) null;
You could do the same with your original code - casting the null is
probably simpler than casting the calculation.
Jon