Casting null values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Microsoft Newsserver

    Casting null values

    Hi,

    Im having a bit of difficulty working out how to do the following.


    method ( paramType? param1, DateTime? requiredByDate , paramType? param3)


    I have a row property which I need to pass to this method, this has a
    property of IsNull which is a boolean, so I tried to do the following

    method ( param1,
    row.IsRequiredB yDateNull() ? null, row.RequiredByD ate,
    param3 )

    This fails on compile because it cant cast from a null to a DateTime.

    How can i construct this ?


  • Jon Skeet [C# MVP]

    #2
    Re: Casting null values

    Microsoft Newsserver <me@nowhere.com wrote:
    Im having a bit of difficulty working out how to do the following.
    >
    >
    method ( paramType? param1, DateTime? requiredByDate , paramType? param3)
    >
    >
    I have a row property which I need to pass to this method, this has a
    property of IsNull which is a boolean, so I tried to do the following
    >
    method ( param1,
    row.IsRequiredB yDateNull() ? null, row.RequiredByD ate,
    param3 )
    >
    This fails on compile because it cant cast from a null to a DateTime.
    >
    How can i construct this ?
    Don't cast to DateTime - cast to DateTime? (with the question mark).
    Then it should be fine.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon_skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Microsoft Newsserver

      #3
      Re: Casting null values


      should have read

      row.IsRequiredB yDateNull() ? null : row.RequiredByD ate;





      "Microsoft Newsserver" <me@nowhere.com wrote in message
      news:eVYNxNU1IH A.4704@TK2MSFTN GP05.phx.gbl...
      Hi,
      >
      Im having a bit of difficulty working out how to do the following.
      >
      >
      method ( paramType? param1, DateTime? requiredByDate , paramType?
      param3)
      >
      >
      I have a row property which I need to pass to this method, this has a
      property of IsNull which is a boolean, so I tried to do the following
      >
      method ( param1,
      row.IsRequiredB yDateNull() ? null, row.RequiredByD ate,
      param3 )
      >
      This fails on compile because it cant cast from a null to a DateTime.
      >
      How can i construct this ?
      >

      Comment

      • Microsoft Newsserver

        #4
        Re: Casting null values

        Great thanks Jon


        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:MPG.22c9ce 55cd3129f2daf@m snews.microsoft .com...
        Microsoft Newsserver <me@nowhere.com wrote:
        >Im having a bit of difficulty working out how to do the following.
        >>
        >>
        >method ( paramType? param1, DateTime? requiredByDate , paramType?
        >param3)
        >>
        >>
        >I have a row property which I need to pass to this method, this has a
        >property of IsNull which is a boolean, so I tried to do the following
        >>
        >method ( param1,
        > row.IsRequiredB yDateNull() ? null,
        >row.RequiredBy Date,
        > param3 )
        >>
        >This fails on compile because it cant cast from a null to a DateTime.
        >>
        >How can i construct this ?
        >
        Don't cast to DateTime - cast to DateTime? (with the question mark).
        Then it should be fine.
        >
        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon_skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • =?ISO-8859-2?Q?Micha=B3_Piaskowski?=

          #5
          Re: Casting null values

          On Jun 23, 5:16 pm, "Microsoft Newsserver" <m...@nowhere.c omwrote:
          should have read
          >
          row.IsRequiredB yDateNull() ? null : row.RequiredByD ate;
          >
          "Microsoft Newsserver" <m...@nowhere.c omwrote in message
          >
          news:eVYNxNU1IH A.4704@TK2MSFTN GP05.phx.gbl...
          >
          Hi,
          >
          Im having a bit of difficulty working out how to do the following.
          >
          method ( paramType? param1, DateTime? requiredByDate , paramType?
          param3)
          >
          I have a row property which I need to pass to this method, this has a
          property of IsNull which is a boolean, so I tried to do the following
          >
          method ( param1,
          row.IsRequiredB yDateNull() ? null, row.RequiredByD ate,
          param3 )
          >
          This fails on compile because it cant cast from a null to a DateTime.
          >
          How can i construct this ?
          I once had the same problem.
          I can't check it with a compiler right now, but the solution is
          something like this:

          row.IsRequiredB yDateNull() ? (DateTime?)null : row.RequiredByD ate;

          Comment

          • Microsoft Newsserver

            #6
            Re: Casting null values

            yes, your absolutely correct. In fact Jon Skeet came up with the same
            solution. For some reason , I never thought of casting to a nullable type.
            DOH!

            Thanks for your help.


            "Michal Piaskowski" <piaskal@gmail. comwrote in message
            news:ac8445c2-574a-4cd2-a27e-addabad89dff@d4 5g2000hsc.googl egroups.com...
            On Jun 23, 5:16 pm, "Microsoft Newsserver" <m...@nowhere.c omwrote:
            >should have read
            >>
            >row.IsRequired ByDateNull() ? null : row.RequiredByD ate;
            >>
            >"Microsoft Newsserver" <m...@nowhere.c omwrote in message
            >>
            >news:eVYNxNU1I HA.4704@TK2MSFT NGP05.phx.gbl.. .
            >>
            Hi,
            >>
            Im having a bit of difficulty working out how to do the following.
            >>
            method ( paramType? param1, DateTime? requiredByDate , paramType?
            param3)
            >>
            I have a row property which I need to pass to this method, this has a
            property of IsNull which is a boolean, so I tried to do the following
            >>
            method ( param1,
            row.IsRequiredB yDateNull() ? null,
            row.RequiredByD ate,
            param3 )
            >>
            This fails on compile because it cant cast from a null to a DateTime.
            >>
            How can i construct this ?
            >
            I once had the same problem.
            I can't check it with a compiler right now, but the solution is
            something like this:
            >
            row.IsRequiredB yDateNull() ? (DateTime?)null : row.RequiredByD ate;

            Comment

            Working...