Reputation: 183
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
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
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