Bug????

Raddict

Active member
Joined
May 19, 2005
Messages
28
Location
Rotterdam (Holland)
Programming Experience
3-5
Hi there,

I was experimenting with VB.NET and couldn't get my code to work properly.

VB.NET:
 If (coordtemp(3) <> coordinate(3)) And (coordtemp(1) <> coordinate(1)) Then
msgbox("there not the same")
End If

when coordtemp(3) is not equal to coordinate(3) AND coordtemp(1) is not equal to coordinate(1) the MSGBox should appear.
The weird thing is that it didn't work like that, VB.NET used the AND as an OR and vice versa :confused:

Is this a known bug? Or am I losing it?
 
Both elements would have to NOT match at the same time for the msgbox to appear.... if one of them does match, then it isn't going to appear.

Tg
 
I know, but the msgbox appears when only one of them does not match.

When I use OR instead of AND it works the way I want it to, if both are not the same the box appears.

It's the world upside down!!!
 
Then it must be that your values are not like you expect to be.

i.e.
VB.NET:
 Dim int1 AsInteger = 1 
 
Dim int2 AsInteger = 2
 
Dim int3 AsInteger = 3
 
Dim int4 AsInteger = 4
 
If int1 <> int2 AndAlso int3 <> int4 Then
 
MessageBox.Show("there not the same")
 
End If


Works just fine ... Cheers ;)

p.s. set int4 to 3 and check again ... msgBox will not appear
 
Hi Kulrom,

according to the debugger both coordtemp(1) and coordinate(1) are both "8.925" and coordtemp(3) and coordinate(3) "1.5".

And your post does not explain why:
coordtemp(1) <> coordinate(1) OR coordtemp(3) <> coordinate(3)

does not cause the msgbox to popup when coordtemp(1) and coordinate(1) are both "8.925" and coordtemp(3) and coordinate(3) are both different, while it should :confused:
 
Ok do the next ... only for test:

PHP:
 Dim temp1() AsDouble = {"9.85"} 
 
Dim temp2() AsDouble = {"9.85"}
 
Dim temp3() AsDouble = {"1.26"}
 
Dim temp4() AsDouble = {"1.25"}
 
If temp1(0) <> temp2(0) Or temp3(0) <> temp4(0) Then
 
MessageBox.Show("One of the value doesn't match condition") 'this should appear if my assumption are well.
 
Else
 
MessageBox.Show("All values don't match another one")
 
EndIf
Cheers ;)

btw, i don't see anything for explanation here ... sorry
 
This doesn't really address your issue, but the only time you should ever use And and Or is when your expressions contain functions that must be executed regardless of the result. Otherwise, as in the case when you are simply comparing variables, you should always use AndAlso and OrElse. And and Or evalute both expressions in all cases. AndAlso and OrElse use a method called "short-circuiting" and only evaluate the second expression if necessary. In most cases it doesn't really matter, but for those who are pedantic about "correctness"...

If you think that it would never matter, let me give you an example where it would. Let's say that if you already have an instance of a form object you want to activate it, but if not you want to create a new one. You would use code something like this:
VB.NET:
If myForm Is Nothing OrElse myForm.IsDisposed Then
	myForm = new Form
	myForm.Show()
Else
	myForm.Activate()
End If
This code will handle the case where the form is yet to be instantited. If you change the OrElse to Or you will get an unhandled exception in that case because you try to access an instance member (IsDisposed) without an instance.
 
Back
Top