vb.net 布尔值和无
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19931669/
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 Boolean and Nothing
提问by Arthur Rey
Let's define this function :
让我们定义这个函数:
Public Function Test(ByVal value As Boolean)
Return "blabla" + If(value = Nothing, "", If(value, "1", "0"))
End Function
I want it to do the following :
Test(True) -> "blabla1", Test(False) -> "blabla0", Test(Nothing) -> "blabla".
我希望它执行以下操作:
Test(True) -> "blabla1", Test(False) -> "blabla0", Test(Nothing) -> "blabla"。
Problem is that Test(Nothing)returns "blabla0".
问题是Test(Nothing)返回“blabla0”。
回答by Guffa
A Booleanvalue can never be null(Nothing), the values that are possible are Trueand False. You need a nullable value, a Boolean?, for it to be able to be null.
甲Boolean值不能null(Nothing),即是可能的值是True和False。您需要一个可为空的值 a Boolean?,以便它能够为空。
Use the HasValueand Valueproperties of the nullable value to check if there is a value, and get the value:
使用可空值的HasValue和Value属性来检查是否有值,并获取值:
Public Function Test(ByVal value As Boolean?)
Return "blabla" + If(Not value.HasValue, "", If(value.Value, "1", "0"))
End Function
回答by Steven Doggart
Booleanis a value type, not a reference type. Therefore, the value of a Booleanvariable can never be Nothing. If you compare a Booleanto Nothing, VB.NET first converts Nothingto the default value for a Boolean, which is False, and then compares it to that. Therefore, testing to see if a Booleanvariable Is Nothingis effectively the same as testing to see if it equals False. If you need a Booleanwhich can be set to Nothing, you need to make it a Nullable(Of Boolean). There is a shortcut for that, though. To make any value type nullable, you can just add a question mark after the type, like this:
Boolean是值类型,而不是引用类型。因此,Boolean变量的值永远不可能是Nothing。如果将 aBoolean与进行比较Nothing,VB.NET 首先转换Nothing为 a 的默认值Boolean,即False,然后将其与该值进行比较。因此,测试一个Boolean变量Is Nothing是否有效与测试它是否等于False. 如果你需要一个Boolean可以被设置为Nothing,你需要使它成为一个Nullable(Of Boolean)。不过,这有一条捷径。要使任何值类型可以为空,您只需在类型后添加一个问号,如下所示:
Public Function Test(ByVal value As Boolean?)
Return "blabla" + If(value.HasValue, If(value.Value, "1", "0"), "")
End Function
As you'll notice, even with the variable being nullable, you still don't test whether or not it is null by comparing it to Nothing. It may come as a surprise, but Nullable(Of T)is actually a value type as well. So, rather than testing to see if it's Nothing, you should use it's HasValueproperty, as I demonstrated in the example.
您会注意到,即使变量可以为空,您仍然不会通过将其与Nothing. 它可能会令人惊讶,但Nullable(Of T)实际上也是一种值类型。因此,与其测试它是否是Nothing,您应该使用它的HasValue属性,正如我在示例中演示的那样。
回答by Kogitsune
When testing against Nothing, you need to use the Is keyword, but you won't be able to pass your value as a Boolean since it only works on reference types.
在针对 Nothing 进行测试时,您需要使用 Is 关键字,但您将无法将您的值作为布尔值传递,因为它仅适用于引用类型。
If( value Is Nothing, "", If( value, "1", "0" ) )
It is also worth noting that Nothing defaults to the default value if it is not a reference type.
还值得注意的是,Nothing 如果不是引用类型,则默认为默认值。
You can change your signature to expect an Object rather than a Boolean if you want to do this.
如果你想这样做,你可以改变你的签名以期望一个对象而不是一个布尔值。

