Decimal nullable problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    Decimal nullable problem

    Hi all,
    I have an expression like this:

    decimal? ValoreComplessi voNettoDelfondo Value = (totaleAttivita Value.HasValue
    || totalePassivita Value.HasValue) ? ((totaleAttivit aValue ?? 0) -
    (totalePassivit aValue ?? 0)) : null;

    where every variable is of type Decimal?.

    But VS 2005 returns me the error "Impossibil e implicit conversion between
    decimal and null".

    Where's the problem?

    Thanks a lot.
    --
    Luigi
  • Hans Kesting

    #2
    Re: Decimal nullable problem

    It happens that Luigi formulated :
    Hi all,
    I have an expression like this:
    >
    decimal? ValoreComplessi voNettoDelfondo Value = (totaleAttivita Value.HasValue
    >>totalePassivi taValue.HasValu e) ? ((totaleAttivit aValue ?? 0) -
    (totalePassivit aValue ?? 0)) : null;
    >
    where every variable is of type Decimal?.
    >
    But VS 2005 returns me the error "Impossibil e 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 "(totalePassivi taValue ?? 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


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Decimal nullable problem

      On Sep 25, 9:52 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
      I have an expression like this:
      >
      decimal? ValoreComplessi voNettoDelfondo Value = (totaleAttivita Value.HasValue
      || totalePassivita Value.HasValue) ? ((totaleAttivit aValue ?? 0) -
      (totalePassivit aValue ?? 0)) : null;
      >
      where every variable is of type Decimal?.
      >
      But VS 2005 returns me the error "Impossibil e 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

      Comment

      • =?Utf-8?B?THVpZ2k=?=

        #4
        Re: Decimal nullable problem

        "Hans Kesting" wrote:
        use "(decimal?)null " instead of "null", then it will compile.
        >
        The problem is that "(totalePassivi taValue ?? 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

        Comment

        • =?Utf-8?B?THVpZ2k=?=

          #5
          Re: Decimal nullable problem

          "Jon Skeet [C# MVP]" wrote:
          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

          Comment

          Working...