"Date Is Nothing" fails

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

apparently references of type Date can not assume the value Nothing, because
the following code fails:

Dim d As Date
.......other code......
If d Is Nothing Then

End If

so then what is the equivalent to Nothing for a reference of type Date ?
 
Date is a value type and value types cannot be null. Think of another way
to identify if the d variable has been given a specific value.
 
* "John A Grandy said:
apparently references of type Date can not assume the value Nothing, because
the following code fails:

Dim d As Date
......other code......
If d Is Nothing Then

End If

so then what is the equivalent to Nothing for a reference of type Date ?

'DateTime' is a so called "value type", not a reference type like
classes are.
 
John,
As the other have pointed out Date is a value type, you never really have a
reference to a Date. Unless of course you box the date value, by placing the
date in an object reference, that however is a different matter...

You can however check to see if d = Date.MinValue to see if the date has
been set to a value yet or not.
Dim d As Date
......other code......
If d = Date.MinValue Then

Hope this helps
Jay
 
heh....null dates can be a problem :-)

Try this

Declare at class level:
Private _NullDate As Date = Nothing



Then at procedural level try this:

Dim MyDate As Date = Nothing

Dim ThisDate As Date = Now

If MyDate.CompareTo(Me._NullDate) = 0 Then

MsgBox("Date Is Nothing")

End If

If ThisDate.CompareTo(Me._NullDate) <> 0 Then

MsgBox("this is a valid date")

End If


In otherwords, you can set a date to nothing. But you can not directly
compare that date to nothing using the IS verb. Instead use the DateType's
CompareTo() method to compare your working date to a date already set to
nothing.

:-D
Ibrahim (formerVB-MVP)Malluf
 
Hi John,.

The following produces the message box.

Dim D as Date
D = Nothing
If D = Nothing Then
MsgBox (D)
End If

This works because although Nothing is a null reference and usually
requires the Is keyword, it is also the default for valuetypes. Thus, in an
equality test with a Date,. the Nothing is taken as a default Date.

Regards,
Fergus
 
Hi John,
When I see so many answers, I cannot resist making too an answer, it has to
be something else than all others, this time is was a very hard to do that,
I hope I succeed. It is a little bit an addition to Jay B and Fergus their
answers.
Dim d As Date
......other code......
If d Is Nothing Then

Dim d As Date
Dim mydateSaterdayNight As String
If Not mydateSaterdayNight Is Nothing Then
d = Nothing
'...............
End If

I hope you get a lot more of other responses.

Cor
 
* (e-mail address removed) (Herfried K. Wagner [MVP]) scripsit:
'DateTime' is a so called "value type", not a reference type like
classes are.

Additional information: In VS.NET Whidbey generics will be introduced
and according to notes posted on the internet there will be a generic
'Nullable' type available (in C#: 'System.Nullable<DateTime>'). Maybe
this will make a solution easier to implement in 2004.

;-)
 
Herfried,
'Nullable' type available (in C#: 'System.Nullable<DateTime>'). Maybe

I saw that, I thought of the mass of developers who have struggled with
implementing their own.

System.Data.SqlTypes gives you Nullable types now, unfortunately VB.NET does
not yet have the overloaded operators to really leverage the
System.Data.SqlTypes.

Jay
 
Hi John

Test for MinValue if you have not set your date value to anything it will
generally be the minimum value (I think it is #12:00:00 AM# or something
like that)

eg

dim dt as date
if dt = dt.MinValue then
.....
end if
 
~~ NullableTypes Project Manager

Sounds like the team that designs Execution Devices for Death Row.

[No offence Luca, I hope ;-) ]

Fergus
 
Back
Top