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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12595775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:18:17  来源:igfitidea点击:

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

vb.netvb.net-2010

提问by ProfK

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.

我试图在我的 UI 上设置一个日期范围过滤器,用复选框说明是否应该使用 DateTimePicker 的值,例如

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

Yet setting fromDateto Nothingdoesn't result in it being set to Nothingbut to '12:00:00 AM', and the following Ifstatement incorrectly executes the filter because startDateis not Nothing.

然而,设置fromDateNothing不会导致它被设置为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

How do I really ensure startDategets a value of Nothing?

我如何真正确保startDate获得 的值Nothing

回答by Damien_The_Unbeliever

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.

问题在于它首先检查此作业的右侧,并确定它是DateTime(no ?)类型。然后执行任务。

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

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

As I said in my comment, Nothingcan be more akin to C#'s default(T)rather than null:

正如我在评论中所说,Nothing可以更类似于 C#default(T)而不是null

Nothingrepresents 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.

Nothing表示数据类型的默认值。默认值取决于变量是值类型还是引用类型。

回答by danicomas

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

使用 VB.NET 和 EF 6.X 来保存 null 是:

Dim nullableData As New Nullable(Of Date)

Dim nullableData As New Nullable(Of Date)

回答by Ulf ?kerstedt

In addition to @Damien_The_Unbeliever's fine answer, using New DateTime?also works:

除了@Damien_The_Unbeliever 的好答案,使用New DateTime?也有效:

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.

您可能会发现它看起来有点反直觉,为什么可能CType(Nothing, DateTime?)更可取。

回答by SSS

Use New Dateas a magic number:

使用New Date作为一个神奇的数字:

Dim fromDate As New Date
If fromDatePicker.Checked Then
  fromDate = fromDatePicker.Value
End If
If fromDate <> New Date Then
  list = list.Where(Function(i) i.InvDate.Value >= fromDate.Value)
End If

回答by Raj

        squery = "insert into tblTest values('" & Me.txtCode.Text & "', @Date)"
        Dim cmd = New SqlCommand(squery, con)

        cmd.Parameters.Add("@Date", SqlDbType.DateTime)
        If txtRequireDate.Text = "" Then
            cmd.Parameters("@Date").Value = DBNull.Value
        Else
            cmd.Parameters("@Date").Value = txtRequireDate.Text
        End If