Microsoft KB Archive/190272

= PRB: Semantic Compatibility Problem with Hardtypes in VBScript =

Article ID: 190272

Article Last Modified on 3/3/2005

-

APPLIES TO


 * Microsoft Internet Explorer 3.0
 * Microsoft Internet Explorer 3.01
 * Microsoft Internet Explorer 3.02
 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Information Server 3.0
 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q190272



SYMPTOMS
VBScript has a semantic compatibility difference with Visual Basic for Applications (VBA) with regard to hardtype comparisons. Basically, VBA's comparison semantics are such that non-variant values are "hardtypes," meaning that they cannot change types. During comparison, variants must be coerced to their types. The problem is that in VBScript everything is a variant, and VBScript doesn't remember that it's a "hardtype" value.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
Use the following code to reproduce this behavior: Sub main Dim v1 Dim v2  v1 = "100"   ' variant string v2 = 1000   ' variant integer msgbox (v1 > v2)  ' string variant always > numeric variant msgbox (v1 > CLng(v2)) ' Clng(v2) is hardtype so it is converted ' to string variant End Sub If you run this test in VBA, the first comparison (v1 > v2) is True because variant strings are always greater than variant numbers. In the second comparison (v1 > CLng(v2)), the CLng function returns a hardtype long value; in this case, VBA converts the string variant to the hardtype (long) and performs the comparison, which returns False. Both comparisons return True in VBScript.

This problem will be apparent for every runtime function that returns a variant in VBScript but returns a hardtype/integral in VBA. This includes the numeric conversions (such as CLng), the math functions (which return double in VBA), and so forth.

Keywords: kbscript kbprb KB190272

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.