By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,823 Members | 730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,823 IT Pros & Developers. It's quick & easy.

"Date Is Nothing" fails

P: n/a
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 ?
Nov 20 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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" <johnagrandy-at-yahoo.com> wrote in
news:#w**************@TK2MSFTNGP10.phx.gbl:
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
?


Nov 20 '05 #2

P: n/a
* "John A Grandy" <johnagrandy-at-yahoo.com> scripsit:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
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

"John A Grandy" <johnagrandy-at-yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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 ?

Nov 20 '05 #4

P: n/a
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



"John A Grandy" <johnagrandy-at-yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 ?

Nov 20 '05 #5

P: n/a
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
Nov 20 '05 #6

P: n/a
Cor
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
Nov 20 '05 #7

P: n/a
* hi***************@gmx.at (Herfried K. Wagner [MVP]) scripsit:
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.


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 K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

P: n/a
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl... * hi***************@gmx.at (Herfried K. Wagner [MVP]) scripsit:
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.


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 K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #9

P: n/a
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
"John A Grandy" <johnagrandy-at-yahoo.com> wrote in message
news:#w**************@TK2MSFTNGP10.phx.gbl...
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 ?

Nov 20 '05 #10

P: n/a
Here you can find a solution that work today:
http://nullabletypes.sourceforge.net/

In late 2004 VS.NET Whidbey (.NET Framework 2.0) will give
another general-pourpose solution implemented with
generics that's System.Nullable<T>.

If you wanna compare know more about System.Nullable<T>
and NullableTypes read this:

http://discuss.develop.com/archives/wa.exe?
A2=ind0310e&L=dotnet-cx&T=0&F=&S=&P=2537

Regards, (luKa)
NullableTypes Project Manager

-----Original Message-----
apparently references of type Date can not assume the value Nothing, becausethe 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 ?

.

Nov 20 '05 #11

P: n/a
Luca Minudel wrote:
Regards, (luKa)
NullableTypes Project Manager


That's a job title I'd have problems explaining to my mom & grandmother...
<g>

-- Mark
Nov 20 '05 #12

P: n/a
ROFL
Nov 20 '05 #13

P: n/a
~~ NullableTypes Project Manager

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

[No offence Luca, I hope ;-) ]

Fergus
Nov 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.