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
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
John White
  • 93
  • 1
  • 3
  • 10
  • 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 – Germann Arlington Dec 12 '12 at 14:14
  • @GermannArlington: Don't you mean "&" operator? I don't think there's a problem with the use of "=" here ;-) – Meta-Knight Dec 12 '12 at 14:19
  • @Meta-Knight Yes, I only just noticed that `&` is not a logical operator at all in VB... – Germann Arlington Dec 12 '12 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 '12 at 15:16

3 Answers3

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.

5uperdan
  • 1,481
  • 2
  • 14
  • 30
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.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • yes I want to do AND checks, whats the diff between "and" and "andalso"? – John White Dec 12 '12 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 http://stackoverflow.com/questions/302047/what-is-the-difference-between-and-and-andalso-in-vb-net – keyboardP Dec 12 '12 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 '12 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
Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55