在visual basic上执行所有其他if块



我正在visual studio 2019的visual basic中工作。有一种方法可以执行所有ElseIf块;如果";或者其中一个是真的?if不能用ElseIf,还有其他命令可以用吗?最后一个想法和为什么我不能只做分离的Ifs的解释是,如果所有的语句都是假的,我需要给出一个错误消息,并且我不知道如何在没有ElseIf命令的情况下对它们进行分组,所以这是我现在唯一想做的想法。

所以,这是ElseIf结构,当它发现其中一个语句为真时,它就会停止

If(boolean_expression 1)Then
' Executes when the boolean expression 1 is true 
ElseIf( boolean_expression 2)Then
' Executes when the boolean expression 2 is true 
ElseIf( boolean_expression 3)Then
' Executes when the boolean expression 3 is true 
Else 
' executes when the none of the above condition is true 
End If

所以,我想执行每一个ElseIf,不管前一个是真是假。我希望你能帮我解决这个问题,谢谢!

您可以单独执行每个块,并且仍然检查是否全部为false:

If (expression1) Then 'Do Stuff
If (expression2) Then 'Do Stuff
If (expression3) Then 'Do Stuff
If Not (expression1) AndAlso Not (expression2) AndAlso Not (expression3) Then 'Do Stuff

我认为在您的情况下,它可能更优雅,计算速度更快:

If Not booleanExpr1 AndAlso Not booleanExpr2 AndAlso Not booleanExpr3 Then
'Fire Error Message
Else
'Execute all 3 expressions
End If

最新更新