So how well do you really know VB ?

Posted Fri, Jun 30 2006 11:43 by bill
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
 
 
Filed under:

Comments

# re: So how well do you really know VB ?

Thursday, June 29, 2006 9:59 PM by Ray Turner

Guess: 1 < 2 < 0.5

# re: So how well do you really know VB ?

Thursday, June 29, 2006 10:16 PM by Ray Turner

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

# re: So how well do you really know VB ?

Thursday, June 29, 2006 10:19 PM by bill

Close but no cigar ;)

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

# re: So how well do you really know VB ?

Thursday, June 29, 2006 10:21 PM by bill

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

# re: So how well do you really know VB ?

Friday, June 30, 2006 5:51 AM by Ayende Rahien

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

5 < 5 < 0

# re: So how well do you really know VB ?

Friday, June 30, 2006 7:06 AM by david

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.

# re: So how well do you really know VB ?

Friday, June 30, 2006 7:29 AM by bill

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 ;)

# re: So how well do you really know VB ?

Friday, June 30, 2006 7:30 AM by bill

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 <g>)

# re: So how well do you really know VB ?

Friday, June 30, 2006 8:16 AM by david

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?

# re: So how well do you really know VB ?

Friday, June 30, 2006 8:23 AM by Jim Wooley

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.

# re: So how well do you really know VB ?

Friday, June 30, 2006 8:35 AM by bill

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

# re: So how well do you really know VB ?

Friday, June 30, 2006 8:37 AM by bill

Hi David,

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

# re: So how well do you really know VB ?

Friday, June 30, 2006 8:44 AM by david

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.


# re: So how well do you really know VB ?

Friday, June 30, 2006 8:57 AM by bill

Hey David,

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

# re: So how well do you really know VB ?

Friday, June 30, 2006 10:54 AM by david

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?

# re: So how well do you really know VB ?

Friday, June 30, 2006 11:10 AM by bill

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.

# Software developer

Saturday, July 01, 2006 12:36 AM by Dan Appleman

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

# re: So how well do you really know VB ?

Saturday, July 01, 2006 1:39 AM by bill

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.

<HUGE HINT>
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
</HUGE HINT>







# re: So how well do you really know VB ?

Saturday, July 01, 2006 12:32 PM by Dan Appleman

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.

# re: So how well do you really know VB ?

Saturday, July 01, 2006 8:21 PM by bill

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 ;)
<quote>
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.
</quote>

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