Nullable Convert ? : operand problem

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

    #1

    Nullable Convert ? : operand problem

    Just one question, why wouldn't the following lines compile:

    decimal? decVar = 5;
    int? intVar = decVar == null ? null :
    Convert.ToInt32 (decVar.GetValu eOrDefault());

    The compiler shows the following message: "There is no implicit conversion
    between 'null' and 'int'."

    A similar if-else construction works just fine.

    Mihkel
  • Marc Gravell

    #2
    Re: Nullable Convert ? : operand problem

    As far as the ternary is concerned, it's two values are incompatible,
    since "null" is interpreted as a null object, not the int? missing
    value. You can re-educate it with a cast:

    int? intVar = decVar == null ? (int?) null :
    Convert.ToInt32 (decVar.GetValu eOrDefault());

    Marc

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Nullable Convert ? : operand problem

      On Dec 5, 9:59 am, mihkeltt <mihke...@discu ssions.microsof t.com>
      wrote:
      Just one question, why wouldn't the following lines compile:
      >
      decimal? decVar = 5;
      int? intVar = decVar == null ? null :
      Convert.ToInt32 (decVar.GetValu eOrDefault());
      >
      The compiler shows the following message: "There is no implicit conversion
      between 'null' and 'int'."
      >
      A similar if-else construction works just fine.
      An int isn't convertible to null, and null isn't convertible to int -
      you need to explain to the compiler which type to use that both values
      *can* be converted to:

      int? intVar = decVar==null ? (int?) null : Convert.ToInt32 (...);
      or
      int? intVar = decVar==null ? null : (int?)Convert.T oInt32(...);

      Note that the code can be simplified somewhat:

      However, for this code it's easier to just use:

      int? intVar = (int?) decVar;

      Jon

      Comment

      • Martin Hey

        #4
        Re: Nullable Convert ? : operand problem

        you've to convert the null to a nullable int

        decimal? decVar = 5;
        int? intVar = decVar == null ? (int?)null :
        Convert.ToInt32 (decVar.GetValu eOrDefault());


        "mihkeltt" <mihkeltt@discu ssions.microsof t.comschrieb im Newsbeitrag
        news:D948A17C-74A3-40BD-92D8-D976E41C0A36@mi crosoft.com...
        Just one question, why wouldn't the following lines compile:
        >
        decimal? decVar = 5;
        int? intVar = decVar == null ? null :
        Convert.ToInt32 (decVar.GetValu eOrDefault());
        >
        The compiler shows the following message: "There is no implicit conversion
        between 'null' and 'int'."
        >
        A similar if-else construction works just fine.
        >
        Mihkel

        Comment

        • =?Utf-8?B?bWloa2VsdHQ=?=

          #5
          Re: Nullable Convert ? : operand problem

          then how is this statement different from the first one i've presented:

          int? intVar = null;

          this would work just fine.

          "Marc Gravell" wrote:
          As far as the ternary is concerned, it's two values are incompatible,
          since "null" is interpreted as a null object, not the int? missing
          value. You can re-educate it with a cast:
          >
          int? intVar = decVar == null ? (int?) null :
          Convert.ToInt32 (decVar.GetValu eOrDefault());
          >
          Marc
          >

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Nullable Convert ? : operand problem

            On Dec 5, 12:10 pm, mihkeltt <mihke...@discu ssions.microsof t.com>
            wrote:
            then how is this statement different from the first one i've presented:
            >
            int? intVar = null;
            >
            this would work just fine.
            That's converting from null to int?, which is fine.

            The conditional operator has to decide what the type of the overall
            expression is. It does this by taking the two result types, and seeing
            if it can convert from either one to the other. In this case, it
            can't.

            Jon

            Comment

            • =?Utf-8?B?bWloa2VsdHQ=?=

              #7
              Re: Nullable Convert ? : operand problem

              thanks for the informative and quick reply, Jon.

              Mihkel

              "Jon Skeet [C# MVP]" wrote:
              On Dec 5, 12:10 pm, mihkeltt <mihke...@discu ssions.microsof t.com>
              wrote:
              then how is this statement different from the first one i've presented:

              int? intVar = null;

              this would work just fine.
              >
              That's converting from null to int?, which is fine.
              >
              The conditional operator has to decide what the type of the overall
              expression is. It does this by taking the two result types, and seeing
              if it can convert from either one to the other. In this case, it
              can't.
              >
              Jon
              >

              Comment

              • Ben Voigt [C++ MVP]

                #8
                Re: Nullable Convert ? : operand problem


                "Marc Gravell" <marc.gravell@g mail.comwrote in message
                news:1fdf4779-5ae8-40b1-93b1-9f373e956803@e2 5g2000prg.googl egroups.com...
                As far as the ternary is concerned, it's two values are incompatible,
                since "null" is interpreted as a null object, not the int? missing
                value. You can re-educate it with a cast:
                >
                int? intVar = decVar == null ? (int?) null :
                Convert.ToInt32 (decVar.GetValu eOrDefault());
                Furthermore (and to eliminate precedence confusion), don't compare Nullable
                values to null.

                Instead, use the HasValue property:

                int? intVar = decVar.HasValue ? Convert.ToInt32 (decVar.GetValu eOrDefault()):
                (int?) null;
                >
                Marc

                Comment

                Working...