如果在 Visual Basic 上有块,则执行所有其他

Execute all else if blocks on visual basic

我在 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,不管前任是真还是假。希望您能帮我解决这个问题,谢谢!

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

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