Why can't I check if a 'DateTime' is 'Nothing'?
Solution 1
This is one of the biggest sources of confusion with VB.Net, IMO.
Nothing
in VB.Net is the equivalent of default(T)
in C#: the default value for the given type.
- For value types, this is essentially the equivalent of 'zero':
0
forInteger
,False
forBoolean
,DateTime.MinValue
forDateTime
, ... - For reference types, it is the
null
value (a reference that refers to, well, nothing).
The statement d Is Nothing
is therefore equivalent to d Is DateTime.MinValue
, which obviously does not compile.
Solutions: as others have said
- Either use
DateTime?
(i.e.Nullable(Of DateTime)
). This is my preferred solution. - Or use
d = DateTime.MinValue
or equivalentlyd = Nothing
In the context of the original code, you could use:
Dim d As DateTime? = Nothing
Dim boolNotSet As Boolean = d.HasValue
A more comprehensive explanation can be found on Anthony D. Green's blog
Solution 2
DateTime is a value type, which is why it can't be null. You can check for it to be equal to DateTime.MinValue
, or you can use Nullable(Of DateTime)
instead.
VB sometimes "helpfully" makes you think it's doing something it's not. When it lets you set a Date to Nothing, it's really setting it to some other value, maybe MinValue.
See this question for an extensive discussion of value types vs. reference types.
Solution 3
DateTime is a value type, which means it always has some value.
It's like an integer - it can be 0, or 1, or less than zero, but it can never be "nothing".
If you want a DateTime that can take the value Nothing, use a Nullable DateTime.
Solution 4
Some examples on working with nullable DateTime
values.
(See Nullable Value Types (Visual Basic) for more.)
'
' An ordinary DateTime declaration. It is *not* nullable. Setting it to
' 'Nothing' actually results in a non-null value.
'
Dim d1 As DateTime = Nothing
Console.WriteLine(String.Format("d1 = [{0}]\n", d1))
' Output: d1 = [1/1/0001 12:00:00 AM]
' Console.WriteLine(String.Format("d1 is Nothing? [{0}]\n", (d1 Is Nothing)))
'
' Compilation error on above expression '(d1 Is Nothing)':
'
' 'Is' operator does not accept operands of type 'Date'.
' Operands must be reference or nullable types.
'
' Three different but equivalent ways to declare a DateTime
' nullable:
'
Dim d2? As DateTime = Nothing
Console.WriteLine(String.Format("d2 = [{0}][{1}]\n", d2, (d2 Is Nothing)))
' Output: d2 = [][True]
Dim d3 As DateTime? = Nothing
Console.WriteLine(String.Format("d3 = [{0}][{1}]\n", d3, (d3 Is Nothing)))
' Output: d3 = [][True]
Dim d4 As Nullable(Of DateTime) = Nothing
Console.WriteLine(String.Format("d4 = [{0}][{1}]\n", d4, (d4 Is Nothing)))
' Output: d4 = [][True]
Also, on how to check whether a variable is null (from Nothing (Visual Basic)):
When checking whether a reference (or nullable value type) variable is null, do not use= Nothing
or<> Nothing
. Always useIs Nothing
orIsNot Nothing
.
Solution 5
You can check this like below :
if varDate = "#01/01/0001#" then
' blank date. do something.
else
' Date is not blank. Do some other thing
end if
Related videos on Youtube
Muleskinner
Danish Windows (vb.net) and web developer (asp.net, MySQL, javaScript) living in Granada, Spain. Working for danish HRM&D company. Latest work: http://jobmatchprofile.com http://garudabp.com/?l=enu http://www.egaruda.com
Updated on July 05, 2022Comments
-
Muleskinner almost 2 years
In VB.NET, is there a way to set a
DateTime
variable to "not set"? And why is it possible to set aDateTime
toNothing
, but not possible to check if it isNothing
? For example:Dim d As DateTime = Nothing Dim boolNotSet As Boolean = d Is Nothing
The second statement throws this error:
'Is' operator does not accept operands of type 'Date'. Operands must be reference or nullable types.
-
DCNYAM about 13 yearsIn addition to John Gant's answer below, you can also check if the datetime variable = Nothing (Note the = instead of "is").
-
Muleskinner about 13 yearsThanks, using Dim boolNotSet As Boolean = d = Nothing seems like the most simple solution right now. Interesting with the Nullable casting never seen that before
-
Karthik Ratnam about 13 years@Chris - I think He is using VB
-
Chris Haas about 13 years@Karthik Ratnam, yes he is but it amounts to the same thing and @Marc Gravell's answer gets all of the points across.
-
tsuo euoy about 13 years@Chris Haas: Not really, because of the Nothing keyword which has no equivalent in C#. This is not a duplicate.
-
DavidRR almost 12 years@NYSystemsAnalyst: according to Nothing (Visual Basic), using
= Nothing
or<> Nothing
is not good practice: "When checking whether a reference (or nullable value type) variable is null, do not use= Nothing
or<> Nothing
. Always useIs Nothing
orIsNot Nothing
." -
Muleskinner almost 12 years@DavidRR I believe you mean
Not Is Nothing
-
DavidRR almost 12 years@Muleskinner: I believe that
IsNot Nothing
andNot Is Nothing
are equivalent. From IsNot Operator (Visual Basic): "IsNot is the opposite of the Is operator. The advantage of IsNot is that you can avoid awkward syntax with Not and Is, which can be difficult to read." -
DavidRR almost 12 years@Muleskinner: Here is the list of the keywords in the Visual Studio 2012 edition of Visual Basic. Not sure how long the
IsNot
keyword has been around. -
Muleskinner almost 12 years@DavidRR Sorry my mistake, I see it was introduced in Visual Basic 2005
-
Maddin almost 7 yearsBe careful with using variable = nothing, this can lead to NullReferenceException
-
-
phn over 5 yearsThanks for explaining. It is interesting to note that isNothing(d) does not work but d = Nothing does!