DateTime as a method parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abhishek Srivastava

    #1

    DateTime as a method parameter

    Hello All,

    I am facing a simple problem. I have a method which accepts two
    parameters. Both are of type DateTime.

    However when calling this method there are times when the caller does
    not have a value of DateTime with it. I tried calling the method with
    null params but that didn't work because DateTime is a struct.

    but I don't want to assign any values to it... because eventally the
    call goes to a stored proc and the proc know what values to use when
    the input is null.

    What can I do to overcome this problem. Right now it appears to me
    that I cannot pass a null to it ... but at the same time I don't want
    to assign any values to these params in my code.

    regards,
    Abhishek.
  • Jürgen Beck

    #2
    Re: DateTime as a method parameter

    "Abhishek Srivastava" <abhishes@hotma il.com> schrieb im Newsbeitrag
    news:affe9fc.03 10040418.1e84f0 2b@posting.goog le.com...[color=blue]
    > I am facing a simple problem. I have a method which accepts two
    > parameters. Both are of type DateTime.
    >
    > However when calling this method there are times when the caller does
    > not have a value of DateTime with it. I tried calling the method with
    > null params but that didn't work because DateTime is a struct.
    >
    > but I don't want to assign any values to it... because eventally the
    > call goes to a stored proc and the proc know what values to use when
    > the input is null.
    >
    > What can I do to overcome this problem. Right now it appears to me
    > that I cannot pass a null to it ... but at the same time I don't want
    > to assign any values to these params in my code.[/color]

    Overload the method with only one DateTime-parameter can be a solution.

    Jürgen Beck
    MCSD.NET, MCDBA, MCT



    Comment

    • Jürgen Beck

      #3
      Re: DateTime as a method parameter

      "Abhishek Srivastava" <abhishes@hotma il.com> schrieb im Newsbeitrag
      news:affe9fc.03 10040418.1e84f0 2b@posting.goog le.com...[color=blue]
      > I am facing a simple problem. I have a method which accepts two
      > parameters. Both are of type DateTime.
      >
      > However when calling this method there are times when the caller does
      > not have a value of DateTime with it. I tried calling the method with
      > null params but that didn't work because DateTime is a struct.
      >
      > but I don't want to assign any values to it... because eventally the
      > call goes to a stored proc and the proc know what values to use when
      > the input is null.
      >
      > What can I do to overcome this problem. Right now it appears to me
      > that I cannot pass a null to it ... but at the same time I don't want
      > to assign any values to these params in my code.[/color]

      Overload the method with only one DateTime-parameter can be a solution.

      Jürgen Beck
      MCSD.NET, MCDBA, MCT



      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: DateTime as a method parameter

        Abhishek Srivastava <abhishes@hotma il.com> wrote:[color=blue]
        > I am facing a simple problem. I have a method which accepts two
        > parameters. Both are of type DateTime.
        >
        > However when calling this method there are times when the caller does
        > not have a value of DateTime with it. I tried calling the method with
        > null params but that didn't work because DateTime is a struct.
        >
        > but I don't want to assign any values to it... because eventally the
        > call goes to a stored proc and the proc know what values to use when
        > the input is null.
        >
        > What can I do to overcome this problem. Right now it appears to me
        > that I cannot pass a null to it ... but at the same time I don't want
        > to assign any values to these params in my code.[/color]

        Consider the kind of solutions you'd use if it were an integer - it's
        exactly the same kind of thing. You basically want a way of signalling
        to the method that it shouldn't bother using that parameter. Ways of
        doing that include:

        o Add an extra boolean parameter which says whether or not the DateTime
        parameter is valid

        o Have another method which only takes one DateTime instead

        o Encapsulate the DateTime in a class - look up NullableTypes on Google
        for more info on this

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: DateTime as a method parameter

          Abhishek Srivastava <abhishes@hotma il.com> wrote:[color=blue]
          > I am facing a simple problem. I have a method which accepts two
          > parameters. Both are of type DateTime.
          >
          > However when calling this method there are times when the caller does
          > not have a value of DateTime with it. I tried calling the method with
          > null params but that didn't work because DateTime is a struct.
          >
          > but I don't want to assign any values to it... because eventally the
          > call goes to a stored proc and the proc know what values to use when
          > the input is null.
          >
          > What can I do to overcome this problem. Right now it appears to me
          > that I cannot pass a null to it ... but at the same time I don't want
          > to assign any values to these params in my code.[/color]

          Consider the kind of solutions you'd use if it were an integer - it's
          exactly the same kind of thing. You basically want a way of signalling
          to the method that it shouldn't bother using that parameter. Ways of
          doing that include:

          o Add an extra boolean parameter which says whether or not the DateTime
          parameter is valid

          o Have another method which only takes one DateTime instead

          o Encapsulate the DateTime in a class - look up NullableTypes on Google
          for more info on this

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Bret Mulvey

            #6
            Re: DateTime as a method parameter

            You could use DateTime.MinVal ue to represent a null DateTime. The code that
            consumes this would just have to know to treat DateTime.MinVal ue as a
            special value.

            The Guid type (also a struct) has Guid.Empty which I've used a lot--I think
            DateTime and other value types need the same thing, e.g. Int32.Empty.


            "Abhishek Srivastava" <abhishes@hotma il.com> wrote in message
            news:affe9fc.03 10040418.1e84f0 2b@posting.goog le.com...[color=blue]
            > Hello All,
            >
            > I am facing a simple problem. I have a method which accepts two
            > parameters. Both are of type DateTime.
            >
            > However when calling this method there are times when the caller does
            > not have a value of DateTime with it. I tried calling the method with
            > null params but that didn't work because DateTime is a struct.
            >
            > but I don't want to assign any values to it... because eventally the
            > call goes to a stored proc and the proc know what values to use when
            > the input is null.
            >
            > What can I do to overcome this problem. Right now it appears to me
            > that I cannot pass a null to it ... but at the same time I don't want
            > to assign any values to these params in my code.
            >
            > regards,
            > Abhishek.[/color]


            Comment

            • Bret Mulvey

              #7
              Re: DateTime as a method parameter

              You could use DateTime.MinVal ue to represent a null DateTime. The code that
              consumes this would just have to know to treat DateTime.MinVal ue as a
              special value.

              The Guid type (also a struct) has Guid.Empty which I've used a lot--I think
              DateTime and other value types need the same thing, e.g. Int32.Empty.


              "Abhishek Srivastava" <abhishes@hotma il.com> wrote in message
              news:affe9fc.03 10040418.1e84f0 2b@posting.goog le.com...[color=blue]
              > Hello All,
              >
              > I am facing a simple problem. I have a method which accepts two
              > parameters. Both are of type DateTime.
              >
              > However when calling this method there are times when the caller does
              > not have a value of DateTime with it. I tried calling the method with
              > null params but that didn't work because DateTime is a struct.
              >
              > but I don't want to assign any values to it... because eventally the
              > call goes to a stored proc and the proc know what values to use when
              > the input is null.
              >
              > What can I do to overcome this problem. Right now it appears to me
              > that I cannot pass a null to it ... but at the same time I don't want
              > to assign any values to these params in my code.
              >
              > regards,
              > Abhishek.[/color]


              Comment

              • Luca Minudel

                #8
                Enable null for DateTime, Booleant, etc.


                The NullableTypes enable DateTime, Boolean, Byte, Decimal,
                etc. to accept null value.

                Look at this: http://nullabletypes.sourceforge.net/

                HTH (luKa)
                [color=blue]
                >-----Original Message-----[/color]
                [color=blue]
                >
                >... I tried calling the method with
                >null params but that didn't work because DateTime is a[/color]
                struct.[color=blue]
                >[/color]

                Comment

                Working...