How to code multiple "Or" in Visual Basic?

14,809

Solution 1

The code above should work but check for null or empty string with String.IsNullOrEmpty is more elegant:

 If String.IsNullOrEmpty(txt1.Text) Or _
   String.IsNullOrEmpty(txt2.Text) Or _
   String.IsNullOrEmpty(txt3.Text) Then
        'Do something
    End If

PD: If you use several "OR", all the conditionals will be checked.

If you use OrElse, it will check the conditionals in order and when one it's not true the next conditional statements will not be checked

Solution 2

For or it's not confused. The above works fine.

Share:
14,809
Alex
Author by

Alex

Updated on June 05, 2022

Comments

  • Alex
    Alex almost 2 years

    I heard VB gets confused with multiple logical operators at once, so I'm stuck here. I have 3 textboxes and I want to check if any of them is empty.

    This simple If did not work:

    If txt1.Text = "" Or txt2.Text = "" Or txt3.Text = "" Then -Something-
    

    However it works if I only put two of them to compare.

    Thanks for your answers.