简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM