Edit : Sorry, missed you wanted to compare both TextBox1 and TextBox2 values to one String Value
Right evaluation is already provided by Neolisk :
If TextBox1.Text = "Brown" AndAlso TextBox2.Text = "Brown" Then
EDIT : the following suggestion is wrong..
Replace this
If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then
by this :
If TextBox1.Text = TextBox2.Text Then
EDIT : I leave the explanation here
If you write If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then
, and we suppose that TextBox2.Text is "Brown" what's happening is :
- Since the
&
is for String concatenation in VB, just like +
, TextBox1.Text
will be compared to the chain "Brown" & TextBox2.Text = "Brown"
.
- With
"Brown" & TextBox2.Text = "Brown"
, "Brown" & TextBox2.Text
will be evaluated first, since the &
string concatener takes precedence over the =
boolean evaluation. You'll get the concatenated string "BrownBrown"
(= "Brown").
- But then, you'll have
"BrownBrown" = "Brown"
, and at this stage, the full chain of evaluation is If TextBox1.Text = "BrownBrown" = "Brown" Then
.
TextBox1.Text = "BrownBrown"
will be evaluated first and will return False
. In the end, you're evaluating :
If False = "Brown" Then ' <- Boolean comparison with String Error !
Since you're new to VB, good to know :
Note that AndAlso
has the shorcut feature. If the evaluation on the left part is False, the right part is not evaluated.
To evaluate both left and right, there is the And
comparer. But, honestly, comparing
If False = True And "SameValue" = "SameValue" ' will always return False.
There is NO meaning in comparing the right side if the left is already False
. You ought to know that this is a unique glitch of VB. However, I don't know if it was fixed since. But if not, the same applies for Or
and OrElse
.
=> You'd better use AndAlso
and OrElse
instead of And/Or
from the start.