Home > Enterprise >  Execute all else if blocks on visual basic
Execute all else if blocks on visual basic

Time:12-08

I'm working in visual basic on visual studio 2019. There is a way to execute all ElseIf blocks even if the first "if" or one of the precessor were true? if is not possible with the ElseIf, there is any other command I could use? The final idea and the explanation of why I can't just do separated Ifs is because I need to give a error message if all the statements were false, and I don't know how to group them without the ElseIf command, so this is the only idea I have in mind right now to do so.

So, this is the ElseIf structure, it stops when it finds one of this statements true

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

So, I want to execute every single ElseIf no matters if the predecessor was true of false. I hope you can help me resolve this, thanks!

CodePudding user response:

You can execute each block separate and still check if all is 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

CodePudding user response:

I think in your case it's probably more elegant and computationally faster to do:

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