如何影响布尔值的结果?表达
How to affect to a boolean value the result of a boolean? expression
我想在 VB 中编写与 C# 中等效的代码:
bool? a = whatever;
bool b= (a==true);
VB 编译器不接受这个:
Dim a As Boolean?
Dim b As Boolean = (a = True)
我想在这种情况下,它将 (a = True)
解释为矫揉造作,而我希望它被解释为一种表达。
(a == True)
显然是语法错误。
您可以使用GetValueOrDefault
-方法:
Dim a As Boolean?
Dim b As Boolean = a.GetValueOrDefault()
您也可以使用 CBool
Dim a As Boolean?
Dim b As Boolean = CBool(a = True)
您需要注意 0、Nothing 和 vbNull 之间的区别。
0 是布尔值的默认值。
vbNull 是保留的 Null 值,应转换为 1。
几乎在所有情况下都不会抛出异常。
Dim a As Boolean? = Nothing
Dim b As Boolean? = vbNull
Dim c As Boolean = vbNull
Dim d As Boolean
Print(a = True) 'will throw an Exception
Print(b = True) 'will return True (as vbNull = Int(1))
Print(c = True) 'will return True as the ? is unnecessary on a Boolean as vbNull = Int(1)
Print(d = True) 'will return False as the default value of a Boolean is 0
Print(a.GetValueOrDefault) 'will return False as this handles the Nothing case.
在使用未分配的值时,您应该始终先检查是否为 Nothing(或者遵循良好做法并在使用前设置值)。
Dim a As Boolean?
Dim b As Boolean = IIf(IsNothing(a), False, a)
如果 a 为 Nothing,这将 return 为假,否则 return A.
只有在测试 Nothing 之后才能测试 vbNull,因为 Nothing returns 是所有值的错误。下面的代码将 return False if Nothing 或 vbNull,否则为
Dim a As Boolean?
Dim b As Boolean = IIf(IsNothing(a), False, IIf(a = vbNull, False, a))
注意:您不能使用下面的代码作为测试 a = vbNull 将反对 Nothing 将抛出异常。
Or(IsNothing(a), a = vbNull)
我也会避免在任何实际应用程序中使用 GetValueOrDefault,因为当您开始使用更复杂的数据类型时,默认值不会那么简单,您可能会得到意想不到的结果。恕我直言,测试 IsNothing(或 Object = Nothing,Object Is Nothing)比依赖数据类型的怪癖要好得多。
最佳 做法是确保 a 具有值,您可以使用
Dim a As Boolean? = New Boolean()
Dim b As Boolean = a
我之所以说这是最佳实践,是因为它可以转换为所有 类,而不仅仅是布尔值。请注意,这对于布尔值来说有点矫枉过正。
希望对您有所帮助。
我想在 VB 中编写与 C# 中等效的代码:
bool? a = whatever;
bool b= (a==true);
VB 编译器不接受这个:
Dim a As Boolean?
Dim b As Boolean = (a = True)
我想在这种情况下,它将 (a = True)
解释为矫揉造作,而我希望它被解释为一种表达。
(a == True)
显然是语法错误。
您可以使用GetValueOrDefault
-方法:
Dim a As Boolean?
Dim b As Boolean = a.GetValueOrDefault()
您也可以使用 CBool
Dim a As Boolean?
Dim b As Boolean = CBool(a = True)
您需要注意 0、Nothing 和 vbNull 之间的区别。 0 是布尔值的默认值。 vbNull 是保留的 Null 值,应转换为 1。 几乎在所有情况下都不会抛出异常。
Dim a As Boolean? = Nothing
Dim b As Boolean? = vbNull
Dim c As Boolean = vbNull
Dim d As Boolean
Print(a = True) 'will throw an Exception
Print(b = True) 'will return True (as vbNull = Int(1))
Print(c = True) 'will return True as the ? is unnecessary on a Boolean as vbNull = Int(1)
Print(d = True) 'will return False as the default value of a Boolean is 0
Print(a.GetValueOrDefault) 'will return False as this handles the Nothing case.
在使用未分配的值时,您应该始终先检查是否为 Nothing(或者遵循良好做法并在使用前设置值)。
Dim a As Boolean?
Dim b As Boolean = IIf(IsNothing(a), False, a)
如果 a 为 Nothing,这将 return 为假,否则 return A.
只有在测试 Nothing 之后才能测试 vbNull,因为 Nothing returns 是所有值的错误。下面的代码将 return False if Nothing 或 vbNull,否则为
Dim a As Boolean?
Dim b As Boolean = IIf(IsNothing(a), False, IIf(a = vbNull, False, a))
注意:您不能使用下面的代码作为测试 a = vbNull 将反对 Nothing 将抛出异常。
Or(IsNothing(a), a = vbNull)
我也会避免在任何实际应用程序中使用 GetValueOrDefault,因为当您开始使用更复杂的数据类型时,默认值不会那么简单,您可能会得到意想不到的结果。恕我直言,测试 IsNothing(或 Object = Nothing,Object Is Nothing)比依赖数据类型的怪癖要好得多。
最佳 做法是确保 a 具有值,您可以使用
Dim a As Boolean? = New Boolean()
Dim b As Boolean = a
我之所以说这是最佳实践,是因为它可以转换为所有 类,而不仅仅是布尔值。请注意,这对于布尔值来说有点矫枉过正。
希望对您有所帮助。