So how well do you really know VB ?

okay, so I posted about the If low < x < high Then syntax.  The smart people got it.  But now comes the big question. Under what circumstances will this code give a different result in VB6 compared to VB.NET.
 
If low < x < high Then
  MsgBox (“true”)
Else
  MsgBox(“false”)
End If
 
 


20 Comments so far

  1.   Ray Turner on June 29th, 2006          

    Guess: 1 < 2 < 0.5

  2.   Ray Turner on June 29th, 2006          

    Sorry – Just reread the question
    how about
    -1<0<0.5

  3.   bill on June 29th, 2006          

    Close but no cigar 😉

    IOW: that produces MsgBox “true” in both Vb6 and Vb.NET.

  4.   bill on June 29th, 2006          

    nope, -1<0<0.5 also produces True in both Vb6 and Vb.NET

  5.   Ayende Rahien on June 30th, 2006          

    VB6 = true == -1 (no idea why, though)
    VB.NEt = true == 1

    5 < 5 < 0

  6.   david on June 30th, 2006          

    true = -1 because that’s what true always equalled in basic. technically, it’s any non-zero value, defaulting to -1.

    vb.net changed it for compatibility with other languages.

  7.   bill on June 30th, 2006          

    Hey Ayende,
    No 5 < 5 < 0 gives us "false" in both Vb6 and Vb.NET, because (5 < 5) = false, and false < 0 is also false 😉

  8.   bill on June 30th, 2006          

    hey David,

    in VB6 ,as it is in VB.NET, true = Not False.
    Vb.NET did NOT change the way VB6 works on that front.

    (oh but you are guys are so close )

  9.   david on June 30th, 2006          

    actually, It’s a bit more complicated than I thought. In VB.NET CInt(True) and CType(True, Integer) both equal -1 but System.Convert.ToInt32(True) equals 1.

    I’m guessing VB.NET uses CInt and the like for implicit conversions when Option Strict is Off?

  10.   Jim Wooley on June 30th, 2006          

    How about 5 < 4 < 0. In this case 5 < 4 is evaulated first resulting in False. Assuming VB6 internalizes False as -1 by default and VB.Net internalizes it as 1 we would get differing results when evauating against 0.

  11.   bill on June 30th, 2006          

    hi Jim,

    Absolutely not. False will be 0 in both languages. So 5 < 4 < 0 is (5 < 4) which is False, and then False < 0 which is also False in Vb6 and Vb.NET

  12.   bill on June 30th, 2006          

    Hi David,

    yep, VB uses CInt (which is the same as CType(value,Integer) instead of System.Convert.ToInt32

  13.   david on June 30th, 2006          

    If 5 < 4 < 0 Then... the 1st thing that would happen would be 5 < 4 would be evaluated to False, then converted to an Int of 0 and 0 < 0 = False. ... 3 < 4 < 0 would give True in VB6 because 3 < 4 ends up as -1, but because VB.NET's implicit conversions seem to use CInt rather than System.Convert I can't see how you could get different results between versions. If it used System.Convert then it would be give False.

  14.   bill on June 30th, 2006          

    Hey David,

    Correct so VB.NEt and VB6 are the same for the expression If low < x < high Then when using Integers

  15.   david on June 30th, 2006          

    I give up… you can’t use strings (doesn’t allow implicit conversions between Strings and Booleans in either version – and even if you CStr the 1st comparrison it works the same both ways as long as you have Option Compare set the same in both) or Dates (no less than operator defined for dates and bools in VB.Net – works in vb6 though, which seems to allow implicit conversion of bools to dates while VB.Net doesn’t) and using Booleans, Doubles, etc all seem to work as expected.

    do all 3 variables have to be of the same type?

  16.   bill on June 30th, 2006          

    The 3 variables do not have to be of the same type. BUT the scenario I had in mind is when they are of the same type.
    And no, no variant, Object or UDT nonsense. The three variables are all intrinsic types.

  17.   Dan Appleman on July 1st, 2006          

    I don’t think this is the one you were looking for, but it does produce different results under VB6 (false) and VB .NET (true):

    If 1@ < "1.0000001" < 0 Then MsgBox ("true") Else MsgBox ("false") End If

  18.   bill on July 1st, 2006          

    hey Dan,

    You are right that does produce different results !!! it’s not the one I have in mind though.
    For those interested, the reason Dan’s sample does what it does, is in VB6, the @ symbol represents Currency data type which only supported up to 4 decimal palces. So the “1.0000001” is seen as @1.0000. In VB.NET there is no currency type, instead the @ is a decimal type which has a scaling factor which allows many many more decimal places, so the “1.0000001” can be accurately converted.

    so in VB6, Dan’s code was basically:
    If false < 0 Then, whereas in Vb.NEt it is If True < 0 Then this isn't the scenario I had in mind though. Mine is when the data types are all the same, are all intrinsic types.
    to simplify the scenario I have in mind, it can be simplified to :
    If True < high Then So all you need to do is tell me the type and value(s) for high

  19.   Dan Appleman on July 1st, 2006          

    This is probably the one you’re looking for:
    Dim low, x, high As Byte
    low = 0
    x = 1
    high = &HFF
    If low < x < high Then MsgBox ("true") Else MsgBox ("false") End If Technically I'd consider this a VB6 bug if I'm reading this right.

  20.   bill on July 1st, 2006          

    Yeh !!!! that’s the exact one I had in mind.

    I’d also consider it a bug except that it is actually documented as behaving this way 😉

    If both operands of a binary operator have the same data type, the result has that data type. An exception is Boolean, which is forced to Short.

    http://msdn2.microsoft.com/en-us/library/ms235255.aspx