What does this code do ?

Posted Thu, Jun 29 2006 3:27 by bill
Will the following VB code display True or False ?  Why ?
(and no cheating by actually trying the code and decompiling it)
Option Strict Off
Module Module1
   Sub Main()
      If 2 < 1 < 5 Then
      End If
   End Sub
End Module
Filed under:


# re: What does this code do ?

Wednesday, June 28, 2006 2:47 PM by Geoff Appleby

Well, without running the code, i'd hazard my guess to be:

2 < 1 = false
False < 5 = true
msgbox true.

Now i'll run it and see what happens :)

# re: What does this code do ?

Wednesday, June 28, 2006 5:05 PM by Ayende Rahien

2 < 1 == False
False is either -1 (vb6) or 1 (VB.Net), and anyway this will make false < 5 true.

I assume that setting option strict on will make the compiler complain, though.

# re: What does this code do ?

Wednesday, June 28, 2006 9:54 PM by bill

d'oh, you guys are no fun ;)

Yep, that's pretty much the why.  The what is that 2 < 1 < 5 is evaluated at compile time and the emmitted code is actually:

It True Then
 MsgBox "true"
 MsgBox "false"
End If

# re: What does this code do ?

Thursday, June 29, 2006 8:48 AM by Jim Wooley

Actually, the emitted code is       Interaction.MsgBox("true", MsgBoxStyle.OkOnly, Nothing). The compiler optomizes the expression, determines it will always evaulate to true and simply replaces the expression with the end result as there are no variables present.

# re: What does this code do ?

Thursday, June 29, 2006 9:02 AM by bill

yep, in a release build. in a debug build that  optimisations is not made, but the constant expression is still evaluated at compile time.  So a Debug build looks like:
It True Then
MsgBox "true"
MsgBox "false"
End If

Whereas a release build will further optimise that to:

MsgBox "true"

# re: What does this code do ?

Monday, July 17, 2006 7:46 AM by John Mull

I know that VB does this, particularly VB6.  But why do the .Net versions assign an integer value to a boolean constant?  Is this a hold-over from earlier versions of VB when there was no proper VB type?

I just checked in Python and the expression does return True.  The following code highlights that Python does the same integer equivocation with booleans:

print False*10
print True*10

this returns:

Therefore, in Python, False=0 and True=1.

Wierd stuff.  Maybe necessary to make the Truth Tables work out, but I'd just as soon see the booleans = False or True but not integers.  I see the equation as being either:

(2 < 1) = False
False < 5 should return an error as we are comparing two different types, similar to stating that "A" < 5.

-- or --

(2 < 1) = False
False < 5 = False since the only true equation involving False could be False = False.

Admittedly, the former method seems the most logical to me.  I caught the order of operations just fine, but the underlying integer value of a boolean seems a little awry.  My fav scripting language, Python, does the same, so I can't claim language superiority on this one.  *sigh*  

Any there really good explanations for why False and True have underlying integer values?

-- John Mull
Catawba County Government
Newton, North Carolina, USA