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

后端 未结 5 1210
有刺的猬
有刺的猬 2020-12-10 11:58

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? =         


        
5条回答
  •  旧时难觅i
    2020-12-10 12:10

    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.

提交回复
热议问题