Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a nullable DateTime to null in VB.NET?

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 to Nothing doesn't result in it being set to Nothing but to '12:00:00 AM', and the following If statement incorrectly executes the filter because startDate is not Nothing.

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 of Nothing?

like image 943
ProfK Avatar asked Sep 26 '12 06:09

ProfK


People also ask

How do I assign a null value to a datetime variable in VB net?

“DateTime is a value type (a structure) and can not be set to null or nothing as with all . Net value types. The default value for a datetime value is zeros for the date portion and 12:00:00 AM for the Time portion.”

Can you set datetime to NULL?

Is it possible to set datetime object to null in C#? DateTime is a Value Type like int, double etc. so there is no way to assigned a null value.

Can date data type be NULL?

Bottomline is: Is it fine to have a NULL value on datetime? Yes. If you have a nullable datetime column, then its fine.

What is NULL in Visual Basic?

In Visual Basic 6.0, the Null keyword indicated that a field contained no valid data, and the IsNull function was used to test for Null. In addition, Visual Basic 6 supported Null propagation when Null was used in an expression, the result of the expression would also be Null.


3 Answers

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.

like image 104
Damien_The_Unbeliever Avatar answered Sep 17 '22 19:09

Damien_The_Unbeliever


With VB.NET and EF 6.X to save null is:

Dim nullableData As New Nullable(Of Date)

like image 42
danicomas Avatar answered Sep 17 '22 19:09

danicomas


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.

like image 37
Ulf Åkerstedt Avatar answered Sep 17 '22 19:09

Ulf Åkerstedt