【问题标题】:Execute all else if blocks on visual basic如果 Visual Basic 上的块,则执行所有其他操作
【发布时间】:2022-01-12 04:23:24
【问题描述】:

我在 Visual Studio 2019 上使用 Visual Basic。即使第一个“if”或其中一个前身为真,是否有一种方法可以执行所有 ElseIf 块?如果 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,不管前任是真还是假。希望您能帮我解决这个问题,谢谢!

【问题讨论】:

  • 您仍然可以使用单独的 ifs。如果 if 语句为真(结果 += 1),只需将递增值存储到变量中。然后在最后评估变量的值。如果结果 = 4(或您做了多少条件),则显示您的消息

标签: vb.net visual-studio if-statement


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2016-08-20
    • 2017-06-08
    • 2016-11-26
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2023-02-07
    • 1970-01-01
    相关资源
    最近更新 更多