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

72,478

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.

Share:
72,478
ProfK
Author by

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, 2022

Comments

  • ProfK
    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 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?

  • Damien_The_Unbeliever
    Damien_The_Unbeliever almost 12 years
    What do you think the difference is between New Date and Nothing being assigned to a Date (or DateTime) variable? (hint - there isn't one)
  • SSS
    SSS almost 12 years
    More typing: Dim fromDate As Date = Nothing
  • Damien_The_Unbeliever
    Damien_The_Unbeliever almost 12 years
    Dim fromDate As Date is even less typing and has the same observable effects.
  • SSS
    SSS almost 12 years
    True enough. However, fromDate = New Date is a valid comparison, while fromDate Is Nothing is not. So for consistency I would encourage consistent use of New Date. I also recommend explicitly setting variables to Nothing or New Date or 0 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
    Damien_The_Unbeliever almost 12 years
    fromDate = Nothing is a valid comparison, and uses one less character :-)
  • ProfK
    ProfK almost 12 years
    @SSS Surely we should be moving away from using legacy types/aliases such as Date?
  • ProfK
    ProfK almost 12 years
    As 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
    Damien_The_Unbeliever almost 12 years
    Nullable(Of DateTime) is a reference type. However, at the point in time when Nothing was being converted, on the right hand side, we have this type information: If(Boolean,DateTime,<Nothing>). Since the return type of If has to match the type of one of its expressions, it picked DateTime. It then converts Nothing to a DateTime (creating a default DateTime), and only after that point does it consider the assignment.
  • SSS
    SSS almost 12 years
    I guess that's a style choice too. As I said before, YMMV.
  • Auguste Van Nieuwenhuyzen
    Auguste Van Nieuwenhuyzen about 8 years
    I 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?.