How do I set a nullable DateTime to null in VB.NET?
Solution 1
The issue is that it's examining the right-hand side of this assignment first, and deciding that it is of type DateTime
(no ?
). Then performing the assignment.
This will work:
Dim fromDate As DateTime? = If(fromDatePicker.Checked, _
fromDatePicker.Value, _
CType(Nothing, DateTime?))
Because it forces the right-hand side's type to be DateTime?
.
As I said in my comment, Nothing
can be more akin to C#'s default(T)
rather than null
:
Nothing
represents the default value of a data type. The default value depends on whether the variable is of a value type or of a reference type.
Solution 2
With VB.NET and EF 6.X to save null is:
Dim nullableData As New Nullable(Of Date)
Solution 3
In addition to @Damien_The_Unbeliever's fine answer, using New DateTime?
also works:
Dim fromDate As DateTime? = If(fromDatePicker.Checked, _
fromDatePicker.Value, _
New DateTime?)
You might find that it looks a bit counter intuitive, why perhaps the CType(Nothing, DateTime?)
is preferable.
ProfK
I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET, with SQL Server. I have, in some way or another, been involved in software development for about eighteen years, but always learning something new. At the moment that is WPF and MVVM.
Updated on July 09, 2022Comments
-
ProfK almost 2 years
I am trying to set up a date range filter on my UI, with checkboxes to say whether a DateTimePicker's value should be used, e.g.
Dim fromDate As DateTime? = If(fromDatePicker.Checked, fromDatePicker.Value, Nothing)
Yet setting
fromDate
toNothing
doesn't result in it being set toNothing
but to '12:00:00 AM', and the followingIf
statement incorrectly executes the filter becausestartDate
is notNothing
.If (Not startDate Is Nothing) Then list = list.Where(Function(i) i.InvDate.Value >= startDate.Value) End If
How do I really ensure
startDate
gets a value ofNothing
? -
Damien_The_Unbeliever almost 12 yearsWhat do you think the difference is between
New Date
andNothing
being assigned to aDate
(orDateTime
) variable? (hint - there isn't one) -
SSS almost 12 yearsMore typing:
Dim fromDate As Date = Nothing
-
Damien_The_Unbeliever almost 12 years
Dim fromDate As Date
is even less typing and has the same observable effects. -
SSS almost 12 yearsTrue enough. However,
fromDate = New Date
is a valid comparison, whilefromDate Is Nothing
is not. So for consistency I would encourage consistent use ofNew Date
. I also recommend explicitly setting variables toNothing
orNew Date
or0
or""
to indicate that the uninitialised value is going to be used as a magic number (and to avoid compiler warnings when you hand them as parameters). It's a style preference I guess - makes no difference at the IL level, as you note. -
Damien_The_Unbeliever almost 12 years
fromDate = Nothing
is a valid comparison, and uses one less character :-) -
ProfK almost 12 years@SSS Surely we should be moving away from using legacy types/aliases such as
Date
? -
ProfK almost 12 yearsAs you said in your comment as well, "the two are equivalent for reference types", and Nullable(Of DateTime) is a reference type, but it looks like the cast trick works, thanks.
-
Damien_The_Unbeliever almost 12 years
Nullable(Of DateTime)
is a reference type. However, at the point in time whenNothing
was being converted, on the right hand side, we have this type information:If(Boolean,DateTime,<Nothing>)
. Since the return type ofIf
has to match the type of one of its expressions, it pickedDateTime
. It then convertsNothing
to aDateTime
(creating a defaultDateTime
), and only after that point does it consider the assignment. -
SSS almost 12 yearsI guess that's a style choice too. As I said before, YMMV.
-
Auguste Van Nieuwenhuyzen about 8 yearsI think this is an interesting point, though not sure it's exactly answering the original question ;). Where I'm setting a nullable date to null in VB.NET I'm using
nullableData = New Date?
.