-1

I am sending an email with a application, and am trying to use a 'And' Operator but it is not working.

Code:

If KeyCheck.Checked = True Then
    oMsg.Body += "Requested Peripherals: Key"
ElseIf SledCheck.Checked - True Then
    oMsg.Body += "Requested Peripherals: Sled"
ElseIf KeyCheck.Checked = True And SledCheck.Checked = True Then
    oMsg.Body += "Requested Peripherals: Key and Sled"
End If

The top 2 work fine when its just Key or Sled, but the one with the And is not, will just output 'Key'

Thanks in advance.

Note: I also tried andalso

Calum
  • 1,889
  • 2
  • 18
  • 36
dwb
  • 475
  • 6
  • 31
  • 1
    I know that you've solved it, but there was a typo at line 3: `ElseIf SledCheck.Checked - True Then`. You put `- True` instead of `= True`. :) – Visual Vincent Sep 26 '15 at 22:45
  • @VisualVincent Looking back, I think that was the original whole issue lol. – dwb Sep 30 '15 at 20:09

1 Answers1

2

If you want enter the if when both conditions are true then you should move your double condition check to be the first in the list and then test the other, otherwise, if one between KeyCheck or SledCheck is checked then you enter that If and never reach the last one....

If KeyCheck.Checked AndAlso SledCheck.Checked Then
    oMsg.Body += "Requested Peripherals: Key and Sled"    
ElseIf KeyCheck.Checked Then
    oMsg.Body += "Requested Peripherals: Key"
ElseIf SledCheck.Checked  Then
    oMsg.Body += "Requested Peripherals: Sled"
End If

Notice also that I have used the AndAlso operator. In this particular case if works also with And but AndAlso express better your intention to have both CheckBoxes marked

See AndAlso vs And for details

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thats exactly what I just tried, and having a different issue now, now when the Sled is checked, it gives a blank output – dwb Sep 26 '15 at 20:58
  • 1
    and also the `= True` are obsolete. – Olivier Jacot-Descombes Sep 26 '15 at 20:59
  • 1
    @dwb got it, It is a typo, you have used the - insteed of = when checking for the SledCheck, another reason to avoid the syntax with (boolean = true) as Olivier suggested in its comment – Steve Sep 26 '15 at 21:01
  • Ah so simple! Awesome thanks! And what would be better then using = true/false? – dwb Sep 26 '15 at 21:06
  • 1
    CheckBox.Checked is a boolean, the If statement evaluates its argument expecting a boolean as result, so checking a boolean against a boolean is not well considered. (Albeit I doubt that it makes any difference in the compiled and optimized code) – Steve Sep 26 '15 at 21:08