Index1<Index2<Index3. Is it right?

Abuzar755

New member
Joined
Aug 22, 2008
Messages
3
Programming Experience
Beginner
Hello all,
I am beginner in Vb.Net,
I read from a book that VB.Net has this syntax.

dim Index1, Index2, Index3 as Integer
if Index1<Index2<Index3 then
' Do something
end if.

Vb 2005 compiled this but result was wrong.
If you have any idea about it pleas help me,
 
How is the result wrong? According to that code all 3 'Index' variables are 0 (zero) so the if statement will produce a False and skip to the end, if any one of those higher than the other two, it should produce a True and run the code in the If statement
 
If your expectation is that the "Do Something" code would be reached then the result was right, it just didn't meet your expectation.

By declaring index1, 2 and 3 using:
VB.NET:
Dim index1, index2, index3 as Integer
you have implicitly set all three values to "0".

Since all the values are the same, none is "less than" the other, resulting in your If statement 'failing' and the code continuing on.

If you want to see the application reach the "Do Something" then mix your declarations up a bit like:

VB.NET:
Dim index1 as Integer = 1
Dim index2 as Integer = 2
Dim index3 as Integer = 3

Additionally, you mentioned that this code compiled, but I am unaware of VB2005 supporting a triple conditional like the:

VB.NET:
if index1<index2<index3

which you show.

I think rather your If would look like
VB.NET:
If (index1 < index2) AND (index2 < index3) then



But, perhaps you have some way of making yours work :)
 
In code i didn't mentioned that

Dim Index1 as Integer = 5
Dim Index1 as Integer = 4
Dim Index1 as Integer = 10

and
Index1<Index2<index3

this returned true
 
I did a quick code test too and found that if either Index1 or Index2 is smaller than Index3 it'll pass the If statement, which doesn't make sense if Index2 is smaller than Index3 but Index2 is larger than Index1
I'm not sure how the CLR is interpreting the If logic for (Index1 < Index2 < Index3)
For example, the following returns True:
VB.NET:
Dim Index1, Index2, Index3 As Integer
Index1 = 2
Index2 = 2
Index3 = 3
If Index1 < Index2 < Index3 Then
  'This code will run when compiled this way
End If
 
IF 5<6<10 then
'it is true
end if
and
if 5<4<10 then
' it is also true
end if

Then can we say that compiler work wrong?
 
IF 5<6<10 then
'it is true
end if
and
if 5<4<10 then
' it is also true
end if

Then can we say that compiler work wrong?
No, the compiler works fine. Here's what's happening:
5 < 4 < 10, the compiler looks at the 5 < 4 part first and produces a boolean False, so now the equation is:
False < 10, since Booleans are integers behind the scenes (True is -1, False is 0) the equation is actually:
0 < 10, which is true so the code runs in that If block.

The compiler works fine in deed, here's the code I used for double checking this before I posted here:
VB.NET:
Module Module1

    Public Sub Main()
        MessageBox.Show(IIf(False < 10, "True", "False"))
    End Sub

End Module
 
I dont think booleans are "represented behind the scenes as integers" - its more likely that the compiler is engaging in something called coercion in an attempt to be helpful. In this case, it's merely causing confusion and can be deemed a Silly Thing To Do.

I'd recommend switching off whichever VB default noddy-setting causes automatic conversion between booleans and integers.. but I dont know which one it is (I fiddled with a few settings but couldnt prevent this foolishness)

False < 10 should raise an error like "Operator < cannot be applied to operands of 'bool' and 'int' " then we wouldnt have any of this confusion - as it is, you'll probably find that the bool is being CType()d into an Int.. Yuck!

If bools were ints behind the scenes, then a DirectCast would not fail with the message "Value of type 'Boolean' cannot be converted to 'Integer' "

To the OP, might i recommend Int1 < Int2 AndAlso Int2 < Int3

Then can we say that compiler work wrong?
It's VB, and hence it is unsurprising. Though the compiler doesnt "work wrong" it certainly doesnt make your life easier with this obscure bit of behaviour..

C#ough c#ough ;)
 
Last edited:
example said:
If 1 < 2 < 3 Then
Option Strict will simply tell you you can't compare Boolean and Integer and implicitly convert between these data types.
MSDN said:
Boolean Type Does Not Convert to Numeric Type Accurately...
You should never write code that relies on equivalent numeric values for True and False. Whenever possible, you should restrict usage of Boolean variables to the logical values for which they are designed.
Go with cjards suggestion for the solution.
 
Back
Top