加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在VB.NET中将可为空的DateTime设置为null?

发布时间:2020-12-17 00:04:21 所属栏目:大数据 来源:网络整理
导读:我正在尝试在我的用户界面上设置一个日期范围过滤器,其复选框用于说明是否应使用DateTimePicker的值,例如 Dim fromDate As DateTime? = If(fromDatePicker.Checked,fromDatePicker.Value,Nothing) 然而,将fromDate设置为Nothing不会导致它被设置为Nothing,而
我正在尝试在我的用户界面上设置一个日期范围过滤器,其复选框用于说明是否应使用DateTimePicker的值,例如
Dim fromDate As DateTime? = If(fromDatePicker.Checked,fromDatePicker.Value,Nothing)

然而,将fromDate设置为Nothing不会导致它被设置为Nothing,而是设置为’12:00:00 AM’,并且以下If语句错误地执行过滤器,因为startDate不是Nothing.

If (Not startDate Is Nothing) Then
    list = list.Where(Function(i) i.InvDate.Value >= startDate.Value)
End If

我如何确保startDate获得Nothing值?

问题是它首先检查这个赋值的右侧,并确定它是DateTime(没有?)类型.然后执行分配.

这将有效:

Dim fromDate As DateTime? = If(fromDatePicker.Checked,_
                               fromDatePicker.Value,_
                               CType(Nothing,DateTime?))

因为它强制右侧的类型是DateTime?.

正如我在评论中所说,Nothing可能更类似于C#的默认值(T)而不是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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读