1

I'm trying to check the following conditions in my if statement. However even when the conditions are met, the code under the if statement doesn't execute.

If (Gnum(0, 0) = Rnum(0, 0) & Gnum(0, 1) = Rnum(0, 1) & Gnum(0, 2) = Rnum(0, 2)) Then
    Lbl_Msg.Text = "Send Msg"
End If
4
  • I am NOT VB specialist, just a general note: Check the operators usage =, & and their precedence. As far as I can see it: Gnum(0, 0) = Rnum(0, 0) & Gnum(0, 1) = Rnum(0, 1) is evaluated as Gnum(0, 0) = ( Rnum(0, 0) & Gnum(0, 1) ) = Rnum(0, 1) I.e. the middle & gets evaluated first Dec 12, 2012 at 14:14
  • @GermannArlington: Don't you mean "&" operator? I don't think there's a problem with the use of "=" here ;-) Dec 12, 2012 at 14:19
  • @Meta-Knight Yes, I only just noticed that & is not a logical operator at all in VB... Dec 12, 2012 at 14:34
  • 1
    Note: that switching Option Strict On will alert you that something is wrong here (i.e. Your code won't compile)
    – Matt Wilko
    Dec 12, 2012 at 15:16

3 Answers 3

3

I'm not certain if '&' will work as intended here as '&' is for concatenation in vb.net

Try using 'and' instead.

Edit: what vb.net thinks you're trying to do here is concatenating all those variables and checking whether or not that result is equal to true (which it is not going to be). That's why the code inside the if statement is not being executed but also no error is being shown.

1
  • WoW, that was the problem. THANKS DUDE
    – John White
    Dec 12, 2012 at 14:16
2

Presumably you want to do AND checks. Instead of &, try using AndAlso

If (Gnum(0, 0) = Rnum(0, 0) AndAlso Gnum(0, 1) = Rnum(0, 1) AndAlso Gnum(0, 2) = Rnum(0, 2)) Then
    Lbl_Msg.Text = "Send Msg"
end if

& is used to concatenate strings in VB.NET.

3
  • yes I want to do AND checks, whats the diff between "and" and "andalso"?
    – John White
    Dec 12, 2012 at 14:19
  • 1
    And evaluates both sides, whereas AndAlso only evaluates the right hand side if the left hand side is true. Therefore, AndAlso should be faster (although real-life performance may not be noticeable depending on what you're doing). If you're familiar with C#, AndAlso is the equivalent of &&. Bit more info here stackoverflow.com/questions/302047/…
    – keyboardP
    Dec 12, 2012 at 14:29
  • >> although real-life performance may not be noticeable depending on what you're doing << It makes a difference, if the right side is considerably more "expensive" than the left side. Like If x=0 AndAlso ExpensiveFunction(y) < 3, and/or if the left side evaluates to true very rarely
    – igrimpe
    Dec 12, 2012 at 15:43
0
If (Gnum(0, 0) = Rnum(0, 0) AND Gnum(0, 1) = Rnum(0, 1) AND Gnum(0, 2) = Rnum(0, 2)) Then
    Lbl_Msg.Text = "Send Msg"
Else
    Lbl_Msg.Text = "see if this text is written to confirm if your if is true"
End If
1
  • 2
    This doesn't give anything more than the two answers already provided
    – Matt Wilko
    Dec 12, 2012 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.