MattJ - 4 years ago 95
Vb.net Question

# ElseIf statement wont run the whole code VB

Ok so ive been working on a program im making and for some reason when I put a bit of my code into a else if statement, it wont run the statemant at all not even the original If statement, However when I put it in a regular If and else statement it works perfectly.

This bit of code works:

``````  If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
Else
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)

End If

End If
``````

And this bit of code doesnt work:

``````    If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
ElseIf TextBox5.Text > TextBox1.Text & TextBox6.Text < TextBox2.Text Then
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)

End If

End If
``````

Could be a syntax error something, but it doesnt come up with an error at all, runs the code perfectly and all that. I don't know what im doing wrong here.

You should use `AND` instead of `&`

`AND` is the logical operator on the other hand `&` is for joining strings

It is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions.

``````If TextBox5.Text > TextBox1.Text And TextBox6.Text > TextBox2.Text Then 'NE
'Code
ElseIf TextBox5.Text > TextBox1.Text And TextBox6.Text < TextBox2.Text Then
'Code
End If
``````

Note:

If values of `TextBox1.Text =4`, `TextBox6.Text=1` then `TextBox1.Text & TextBox6.Text` will yields `41`

`&` is used to concatenate two strings

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download