我有一段代码让我感到困惑:
If (_campaignIdValue <> "SALESFORCE") And (_campaignIdValue <> "SFNESMORT") And _
(_campaignIdValue <> "SFNESTERM") And (_campaignIdValue <> "SFTENNIS") +
(_campaignIdValue <> "LEADSINIT") And (_campaignIdValue <> "LEADSEXPIR") And _
(_campaignIdValue <> "LEADSRSRCH") Then
' Some code
End If
为什么 if 语句中的“+”是可以的,它是什么意思?
最佳答案
设置Option Strict On
(最好作为默认项目设置),那么你不会得到任何神奇的 bool 转换,但你必须修复这个编译器错误。使用Option Strict Off
(不幸的是默认)我认为Convert.ToBoolean
被调用,这意味着任何不等于零的值都被视为 True
。很奇怪,不是吗?
Dim weirdBool As Boolean = 1 - 1 ' evaluates to False with Strict Off '
weirdBool = 1 + 1 ' evaluates to True with Strict Off '
使用 Option Strict On
你会得到 this meaningful compiler error :
Option Strict On disallows implicit conversions from 'Integer' to 'Boolean'
您还应该使用 AndAlso
而不是 And
和 OrElse
而不是 Or
因为它们是短路的运营商。否则以下将引发意外异常:
Dim str As String = Nothing
If str IsNot Nothing And str.Length > 0 Then
End If
即使 str IsNot Nothing
已经是 False
,这里也会计算双方。
关于vb.net "if statement"包含加号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34314351/