VBScript implicit conversion in IF statement different from variable to literals?

后端 未结 2 2029
既然无缘
既然无缘 2020-12-18 10:36

We are currently having an issue due to implicit conversion in an IF statement in VBScript (Classic ASP) that don\'t do implicit conversion the same way when dealing with a

相关标签:
2条回答
  • 2020-12-18 11:19

    You are (implicitly) declaring your variables As Variant so your If conditions actually test the equality of two Variants and determine that they are unequal.

    In the last cases, however, you are using String constants (which can never be Variant, even if declared without a type) and String literals.

    My guess is that when you compare two Variants, VB first determines whether they have the same type tag and if they don’t, resolves to False.

    0 讨论(0)
  • 2020-12-18 11:22

    This is the result of one documented behavior and one undocumented one.


    The documented behavior is that in comparisons, a number is always less than a string. This is mentioned in the documentation for Comparison Operators. Paraphrasing the table near the bottom of the page:

    If one expression is numeric and the other is a string, then the numeric expression is less than the string expression.


    The undocumented behavior is that comparisons involving literals are handled differently from comparisons involving variables. See this blog entry for more details. To summarize the important conclusion:

    The relevant comparison rules in VB6/VBScript go like this:

    • Hard string ~ hard number: convert string to number, compare numbers
    • Hard string ~ soft number: convert number to string, compare strings
    • Soft string ~ hard number: convert string to number, compare numbers
    • Soft string ~ soft number: any string is greater than any number

    The documented behavior explains why the first two comparisons are false, while the undocumented behavior explains why the last two comparisons are true.

    0 讨论(0)
提交回复
热议问题