Results 1 to 7 of 7

Thread: Simple IF discrepancy :S

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    Simple IF discrepancy :S

    Look at the following code

    VB Code:
    1. If 36 Then MsgBox "START"
    2.     If 65 Then MsgBox "END"
    3.     If (36) And (65) Then MsgBox "BOTH"

    The first two IF statements validate. However, the last one does not. Why is it doing that? Isn't that how AND is suppose to work?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    Re: Simple IF discrepancy :S

    Ok so I've isolated the problem quickly to something that is sooo stupid, who ever programmed it should be shot.

    If 35 And 65 Then MsgBox "TEST" -> validates
    If 37 And 65 Then MsgBox "TEST" -> validates

    If you noticed, the AND statement validates WHEN both integers are odd or both are even :S. WHAT THE HELL IS THAT ABOUT? I didn't even know any language does such a useless validation :S.

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Simple IF discrepancy :S

    Quote Originally Posted by INF3RN0666
    Ok so I've isolated the problem quickly to something that is sooo stupid, who ever programmed it should be shot.
    VB isn't doing anything wrong. You are.

    'And' performs a bit-comparison of two numbers. to use your examples:
    Code:
    1000001   - 65 in binary
    0100100   - 36 in binary
    -------
    0000000   - if both bits are 1 we'll get a 1 in the resulting bit position else we get a 0
    
    the above comes out as 0 which is False
    
    1000001   - 65 in binary
    0100101   - 37 in binary
    -------
    0000001   - the result is 1, which is Not False and hence True
    what are you trying to achieve?

  4. #4
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Simple IF discrepancy :S

    VB Code:
    1. If 36 Then MsgBox "START"
    2. If 65 Then MsgBox "END"
    3. If (36) And (65) Then MsgBox "BOTH"

    Try this:
    VB Code:
    1. If 36 Then
    2.     MsgBox "START"
    3. ElseIf 65 Then
    4.     MsgBox "END"
    5. Else
    6.     MsgBox "BOTH"
    7. End If

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    Re: Simple IF discrepancy :S

    OHHH bit comparison. I thought it evaluated each term on its own to 1 or 0, then it compared both. DAMN! I was doing InStr in a function and I usually put InStr > 0 but I realized that it should work either way. Now I understand why to put > 0. Thx for your help.

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Simple IF discrepancy :S

    well you can use a single InStr on it's own like that:
    VB Code:
    1. If InStr("aaaa", "a") Then
    but once you introduce multiple terms with And / Or / etc. then each part needs to be a Boolean, otherwise you won't get the right results.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Simple IF discrepancy :S

    Errr to what variable are you comparing the values against? Those statements will always return True.

    EDIT: Or try bushmobile's suggestion

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width