|
-
Jun 30th, 2006, 11:54 AM
#1
Thread Starter
Addicted Member
Simple IF discrepancy :S
Look at the following code
VB Code:
If 36 Then MsgBox "START"
If 65 Then MsgBox "END"
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?
-
Jun 30th, 2006, 11:57 AM
#2
Thread Starter
Addicted Member
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.
-
Jun 30th, 2006, 12:22 PM
#3
Re: Simple IF discrepancy :S
 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?
-
Jun 30th, 2006, 12:23 PM
#4
PowerPoster
Re: Simple IF discrepancy :S
VB Code:
If 36 Then MsgBox "START"
If 65 Then MsgBox "END"
If (36) And (65) Then MsgBox "BOTH"
Try this:
VB Code:
If 36 Then
MsgBox "START"
ElseIf 65 Then
MsgBox "END"
Else
MsgBox "BOTH"
End If
-
Jun 30th, 2006, 12:33 PM
#5
Thread Starter
Addicted Member
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.
-
Jun 30th, 2006, 12:50 PM
#6
Re: Simple IF discrepancy :S
well you can use a single InStr on it's own like that:
VB Code:
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.
-
Jun 30th, 2006, 12:53 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|