0

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!

Mary
  • 14,926
  • 3
  • 18
  • 27
  • 1
    You can still user separate ifs. Just store an incrementing value to a variable if an if statement is true (result += 1). Then evaluate the value of the variable at the end. If result = 4 (or how many conditions you did), then show your message – F0r3v3r-A-N00b Dec 07 '21 at 03:52

2 Answers2

1

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
  • You might be interested in [(OrElse and Or) and (AndAlso and And) - When to use?](https://stackoverflow.com/q/8409467/1115360) – Andrew Morton Dec 07 '21 at 14:32
1

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
Lorenzo Martini
  • 373
  • 1
  • 8