Home All Groups Group Topic Archive Search About

best way to tell if a date is not assigned?

Author
14 Mar 2006 9:26 PM
Brian Henry
I want to tell if a date is invaid (not assigned yet)

right now i am doing this

dim aDate as datetime


if aDate.date = #12:00:00 AM#
    ' not assigned
else
    'assigned
end if

which seems to work, but is this the best way to do this?

Author
14 Mar 2006 9:32 PM
Marina Levit [MVP]
I would compare it with DateTime.MinValue instead of hardcoding what you
have there.

Show quoteHide quote
"Brian Henry" <nospam@nospam.com> wrote in message
news:exCS236RGHA.4300@TK2MSFTNGP14.phx.gbl...
>I want to tell if a date is invaid (not assigned yet)
>
> right now i am doing this
>
> dim aDate as datetime
>
>
> if aDate.date = #12:00:00 AM#
>    ' not assigned
> else
>    'assigned
> end if
>
> which seems to work, but is this the best way to do this?
>
Author
15 Mar 2006 11:37 AM
Carlos J. Quintero [VB MVP]
If you are using .NET 1.x, you have to use a magic value as you are doing.
You can use Date.MinValue since it is unlilely that you use it.

If you are using .NET 2.0 you can use the new nullable value types:

      Dim dtDate As Nullable(Of Date)

      If dtDate.HasValue Then
         MessageBox.Show(dtDate.Value.ToString)
      Else
         MessageBox.Show("No date")
      End If

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Show quoteHide quote
"Brian Henry" <nospam@nospam.com> escribió en el mensaje
news:exCS236RGHA.4300@TK2MSFTNGP14.phx.gbl...
>I want to tell if a date is invaid (not assigned yet)
>
> right now i am doing this
>
> dim aDate as datetime
>
>
> if aDate.date = #12:00:00 AM#
>    ' not assigned
> else
>    'assigned
> end if
>
> which seems to work, but is this the best way to do this?
>
Author
15 Mar 2006 12:23 PM
Brian Henry
wow didn't know abou the nullable types... definatly going to have to look
into that... now if the controls just were nullable like in a date time
picker and such...
Author
15 Mar 2006 1:16 PM
Carlos J. Quintero [VB MVP]
Hi Brian,

Nullable types are new in .NET 2.0 using the new generics (Of T)
capabilities. In .NET 1.x you had to use magic values or implement your own
NullableDate type (there were some implementations out there). For backwards
compatibility the controls may not accept nullable types, but you can extend
them easily with a user control and a new property. The datetime picker has
a ShowCheckbox property or similar.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Show quoteHide quote
"Brian Henry" <nospam@nospam.com> escribió en el mensaje
news:O7C19sCSGHA.524@TK2MSFTNGP10.phx.gbl...
> wow didn't know abou the nullable types... definatly going to have to look
> into that... now if the controls just were nullable like in a date time
> picker and such...
>