Alex
Alex

Reputation: 183

How to code multiple "Or" in Visual Basic?

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.

Upvotes: 2

Views: 18563

Answers (3)

codingbiz
codingbiz

Reputation: 26386

Your code works. If you want the rest of the check to be ommitted you can use OrElse

  If txt1.Text = "" OrElse txt2.Text = "" OrElse txt3.Text = "" Then 

  End If

or better

  If String.IsNullOrEmpty(txt1.Text) OrElse String.IsNullOrEmpty(txt2.Text) OrElse String.IsNullOrEmpty(txt3.Text) Then 

  End If

Upvotes: 0

Asken
Asken

Reputation: 8051

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

Upvotes: 3

Carlos Landeras
Carlos Landeras

Reputation: 11063

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

Upvotes: 5

Related Questions