VB.NET - 可为空的日期时间和三元运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4189876/
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
VB.NET - Nullable DateTime and Ternary Operator
提问by anonymous
I'm having problems with a Nullable DateTime in VB.NET (VS 2010).
我在 VB.NET (VS 2010) 中遇到了 Nullable DateTime 问题。
Method 1
方法一
If String.IsNullOrEmpty(LastCalibrationDateTextBox.Text) Then
gauge.LastCalibrationDate = Nothing
Else
gauge.LastCalibrationDate = DateTime.Parse(LastCalibrationDateTextBox.Text)
End If
Method 2
方法二
gauge.LastCalibrationDate = If(String.IsNullOrEmpty(LastCalibrationDateTextBox.Text), Nothing, DateTime.Parse(LastCalibrationDateTextBox.Text))
When given an empty string Method 1 assigns a Null (Nothing) value to gauge.LastCalibrationDate but Method 2 assigns it the DateTime.MinValue.
当给定一个空字符串时,方法 1 会为 Gauge.LastCalibrationDate 分配一个 Null(无)值,但方法 2 会为其分配 DateTime.MinValue。
In other places in my code I have:
在我的代码中的其他地方,我有:
LastCalibrationDate = If(IsDBNull(dr("LastCalibrationDate")), Nothing, dr("LastCalibrationDate"))
This correctly assigns Null (Nothing) from a Ternary Operator to a Nullable DateTime.
这将 Null (Nothing) 从三元运算符正确分配给 Nullable DateTime。
What am I missing? Thanks!
我错过了什么?谢谢!
回答by Bob Mc
I will admit that I'm not an expert on this, but apparently it stems from two things:
我承认我不是这方面的专家,但显然它源于两件事:
- The
If
ternary operator can return only one type, in this case a date type, not a nullable date type - The VB.Net
Nothing
value is not actuallynull
but is equivalent to the default value of the specified type, in this case a date, not a nullable date. Hence the date minimum value.
- 在
If
三元运算符只能返回一个类型,在这种情况下,一个日期类型,而不是一个可空日期类型 - VB.Net
Nothing
值实际上不是,null
而是等效于指定类型的默认值,在这种情况下是日期,而不是可以为空的日期。因此日期最小值。
I derived most of the information for this answer from this SO post: Ternary operator VB vs C#: why resolves to integer and not integer?
我从这篇 SO 帖子中获得了这个答案的大部分信息:三元运算符 VB 与 C#:为什么解析为整数而不是整数?
Hope this helps and that someone like Joel Coehoorn can shed more light on the subject.
希望这会有所帮助,并且像 Joel Coehoorn 这样的人可以对这个主题有更多的了解。
回答by Ahmad Mageed
Bob Mc is correct. Pay extra attention to his second point - this isn't the case in C#.
鲍勃·麦克是对的。特别注意他的第二点 - 在 C# 中不是这种情况。
What you need to do is force Nothing
to a nullable DateTime by casting it as follows:
您需要做的是Nothing
通过按如下方式强制转换为可为空的 DateTime:
gauge.LastCalibrationDate = If(String.IsNullOrEmpty(LastCalibrationDateTextBox.Text), CType(Nothing, DateTime?), DateTime.Parse(LastCalibrationDateTextBox.Text))
Here is a snippet to demonstrate:
这是一个片段来演示:
Dim myDate As DateTime?
' try with the empty string, then try with DateTime.Now.ToString '
Dim input = ""
myDate = If(String.IsNullOrEmpty(input), CType(Nothing, DateTime?), DateTime.Parse(input))
Console.WriteLine(myDate)
Instead of casting you can also declare a new nullable: New Nullable(Of DateTime)
or New DateTime?()
. The latter format looks a little odd but it's valid.
除了强制转换,您还可以声明一个新的 nullable:New Nullable(Of DateTime)
或New DateTime?()
。后一种格式看起来有点奇怪,但它是有效的。