null date?

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

    #1

    null date?

    why doesn't c# seem to support some kind of 'null date'

    for instance:

    i have an application where the user is not obliged to enter a date until
    an event happens (e.g. a bug was fixed on xyz)

    the calendar control in asp.net doesn't seem to have a selectedDate = null
    property

    is there any way around this?


    --
    #-)


  • Madhu [MVP]

    #2
    null date?

    Hi,

    The DateTime data type is a value type and not reference
    type.

    For more information about value types and reference
    types, look at these url's

    http://msdn.microsoft.com/library/default.asp?
    url=/library/en-us/csref/html/vcrefvaluetypes .asp

    http://msdn.microsoft.com/library/default.asp?
    url=/library/en-us/csref/html/vcrefvaluetypes .asp

    The bottom line is, the value type cannot hold null
    values unlike reference types.

    So the work arounds which i can think of is to,

    1) Assign a date like 1/1/1900 which will be considered
    as null.
    2) Create a class which has a field of type date. This
    can be done in a smart way, such that you use it like a
    normal value type, i.e., you don't need to use 'new' to
    create the instance of the class.
    This can be acheived by using implicit operator.
    Thus, you get the ease of use of value type and also for
    types like date you can assign null value since the class
    is a reference type.

    Hope this helps!!

    Regards,
    Madhu

    MVP | MCSD.NET
    [color=blue]
    >-----Original Message-----
    >why doesn't c# seem to support some kind of 'null date'
    >
    >for instance:
    >
    >i have an application where the user is not obliged to[/color]
    enter a date until[color=blue]
    >an event happens (e.g. a bug was fixed on xyz)
    >
    >the calendar control in asp.net doesn't seem to have a[/color]
    selectedDate = null[color=blue]
    >property
    >
    >is there any way around this?
    >
    >
    >--
    >#-)
    >
    >
    >.
    >[/color]

    Comment

    • Mitch Denny

      #3
      Re: null date?

      Hiya,

      This is a common challenge for developers with with the split
      reference/value type system. Many developers go for a magic date which
      "means" null but it is far from ideal. You may like to look at the
      NullableTypes project being hosted at SourceForge. It provides wrapper
      classes around the standard structs which are nullable. You will probably
      want to see what impact these have on performance in your application - the
      license terms seem to be OK.

      ----------------------------------------
      - Mitch Denny
      - mitch.denny@mon ash.net
      - http://www.monash.net
      - +61 (414) 610141
      -

      "evolve" <NOngnSPAMpeaSP AMrseNO_S__PAMh otmail.com> wrote in message
      news:uXnXr%23Pq DHA.2804@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > why doesn't c# seem to support some kind of 'null date'
      >
      > for instance:
      >
      > i have an application where the user is not obliged to enter a date until
      > an event happens (e.g. a bug was fixed on xyz)
      >
      > the calendar control in asp.net doesn't seem to have a selectedDate = null
      > property
      >
      > is there any way around this?
      >
      >
      > --
      > #-)
      >
      >[/color]


      Comment

      • Stu Smith

        #4
        Re: null date?

        You could use System.Data.Sql Types.SqlDate


        "evolve" <NOngnSPAMpeaSP AMrseNO_S__PAMh otmail.com> wrote in message
        news:uXnXr%23Pq DHA.2804@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > why doesn't c# seem to support some kind of 'null date'
        >
        > for instance:
        >
        > i have an application where the user is not obliged to enter a date until
        > an event happens (e.g. a bug was fixed on xyz)
        >
        > the calendar control in asp.net doesn't seem to have a selectedDate = null
        > property
        >
        > is there any way around this?
        >
        >
        > --
        > #-)
        >
        >[/color]


        Comment

        • Brian W

          #5
          Re: null date?

          Use the static property DateTime.MinVal ue. (which is 01/01/0001 12:00AM)

          This is considered to be a null date.

          HTH
          Brian w

          "evolve" <NOngnSPAMpeaSP AMrseNO_S__PAMh otmail.com> wrote in message
          news:uXnXr%23Pq DHA.2804@TK2MSF TNGP09.phx.gbl. ..[color=blue]
          > why doesn't c# seem to support some kind of 'null date'
          >
          > for instance:
          >
          > i have an application where the user is not obliged to enter a date until
          > an event happens (e.g. a bug was fixed on xyz)
          >
          > the calendar control in asp.net doesn't seem to have a selectedDate = null
          > property
          >
          > is there any way around this?
          >
          >
          > --
          > #-)
          >
          >[/color]


          Comment

          • Matthew Slyh

            #6
            Re: null date?

            There is one caveat to using DateTime.MinVal ue... the actual date
            (01/01/0001) is not supported by most database vendors. For example, I
            believe that SQL Server throws an exception for dates less than 1/1/1900.
            You can write code to get around this by translating the DateTime.MinVal ue
            to a DbNull value before writing it to the database but this is a little
            troublesome unless you have a good framework setup. For an enterprise
            application, you wouldn't want to rely on individual programmers to
            constantly be checking against the DateTime.MinVal ue every time a database
            access occurs. As some of the other responses pointed out, a wrapper class
            is a good way to avoid some of these issues.

            Hopes this helps....

            "Brian W" <brianw@gold_de ath_2_spam_rush .com> wrote in message
            news:OeyFR1WqDH A.688@TK2MSFTNG P10.phx.gbl...[color=blue]
            > Use the static property DateTime.MinVal ue. (which is 01/01/0001 12:00AM)
            >
            > This is considered to be a null date.
            >
            > HTH
            > Brian w
            >
            > "evolve" <NOngnSPAMpeaSP AMrseNO_S__PAMh otmail.com> wrote in message
            > news:uXnXr%23Pq DHA.2804@TK2MSF TNGP09.phx.gbl. ..[color=green]
            > > why doesn't c# seem to support some kind of 'null date'
            > >
            > > for instance:
            > >
            > > i have an application where the user is not obliged to enter a date[/color][/color]
            until[color=blue][color=green]
            > > an event happens (e.g. a bug was fixed on xyz)
            > >
            > > the calendar control in asp.net doesn't seem to have a selectedDate =[/color][/color]
            null[color=blue][color=green]
            > > property
            > >
            > > is there any way around this?
            > >
            > >
            > > --
            > > #-)
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Ed

              #7
              Re: null date?

              Which is great, but they don't serialize...
              [color=blue]
              >-----Original Message-----
              >You could use System.Data.Sql Types.SqlDate
              >
              >
              >"evolve" <NOngnSPAMpeaSP AMrseNO_S__PAMh otmail.com> wrote[/color]
              in message[color=blue]
              >news:uXnXr%23P qDHA.2804@TK2MS FTNGP09.phx.gbl ...[color=green]
              >> why doesn't c# seem to support some kind of 'null date'
              >>
              >> for instance:
              >>
              >> i have an application where the user is not obliged to[/color][/color]
              enter a date until[color=blue][color=green]
              >> an event happens (e.g. a bug was fixed on xyz)
              >>
              >> the calendar control in asp.net doesn't seem to have a[/color][/color]
              selectedDate = null[color=blue][color=green]
              >> property
              >>
              >> is there any way around this?
              >>
              >>
              >> --
              >> #-)
              >>
              >>[/color]
              >
              >
              >.
              >[/color]

              Comment

              Working...