Decimal nullable problem

  • Thread starter Thread starter Luigi
  • Start date Start date
L

Luigi

Hi all,
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?

Thanks a lot.
 
It happens that Luigi formulated :
Hi all,
I have an expression like this:

decimal? ValoreComplessivoNettoDelfondoValue = (totaleAttivitaValue.HasValue
(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?

Thanks a lot.

use "(decimal?)null" instead of "null", then it will compile.

The problem is that "(totalePassivitaValue ?? 0)" has a resulttype of
"decimal", not "decimal?". So when the compiler tries to decide on a
returntype of the conditional expression (..?..:..) it sees "decimal"
in the "true expression" and a null in the "false expression". And it
can't match the two.
If you cast the null to a nullable decimal, then the compiler sees
"decimal" and "decimal?" and knows there is an implicit conversion, so
the result-type will be "decimal?".

Hans Kesting
 
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
 
Hans Kesting said:
use "(decimal?)null" instead of "null", then it will compile.

The problem is that "(totalePassivitaValue ?? 0)" has a resulttype of
"decimal", not "decimal?". So when the compiler tries to decide on a
returntype of the conditional expression (..?..:..) it sees "decimal"
in the "true expression" and a null in the "false expression". And it
can't match the two.
If you cast the null to a nullable decimal, then the compiler sees
"decimal" and "decimal?" and knows there is an implicit conversion, so
the result-type will be "decimal?".

Thank you Hans, now it's working (with a cast).

Luigi
 
Jon Skeet said:
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.

Well, thank you Jon, another good point of view to keep in mind.

Luigi
 
Back
Top