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

Currently, the problem is due to the implicit conversion in the IF expression to VBScript (Classic ASP), which does not do the implicit conversion in the same way when dealing with a variable or literal. Can someone explain this behavior to me why VBScript acts this way?

Here is an example of what I mean:

Const c_test = 3 Dim iId : iId = 3 Dim iTestStr : iTestStr = "3" If iId = iTestStr Then Response.Write("Long variable = String variable : Equal") Else Response.Write("Long variable = String variable : Not Equal") End If Response.Write("<br/>") If c_test = iTestStr Then Response.Write("Long constant = String variable : Equal") Else Response.Write("Long constant = String variable : Not Equal") End If Response.Write("<br/>") If c_test = iId Then Response.Write("Long constant = Long variable : Equal") Else Response.Write("Long constant = Long variable : Not Equal") End If Response.Write("<br/>") If iId = "3" Then Response.Write("Long variable = String literal : Equal") Else Response.Write("Long variable = String literal : Not Equal") End If Response.Write("<br/>") If c_test = "3" Then Response.Write("Long constant = String literal : Equal") Else Response.Write("Long constant = String literal : Not Equal") End If 

Which operators:

Long variable = String variable: Not equal

Long constant = String variable: Not equal

Long constant = Long variable: Equals

Long variable = String literal: Equal

Long constant = String literal: Equal

Which is pretty confusing o_O

+4
source share
2 answers

You (implicitly) declare your As Variant variables, so your If conditions actually check the equality of the two Variant and determine that they are not equal.

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

My assumption is that by comparing two Variant s, VB first determines if they have a tag type tag, and if not, then False allowed.

+1
source

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


The documented behavior is that when comparing, the number is always less than the string. This is indicated in the documentation for Comparison Operators . To paraphrase the table at the bottom of the page:

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


Undocumented behavior is that comparisons with literals are handled differently than comparisons with variables. See this blog post for more details. To summarize the important conclusion:

The corresponding comparison rules in VB6 / VBScript are as follows:

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

Documented behavior explains why the first two comparisons are false, and undocumented behavior explains why the last two comparisons are true.

+4
source

Source: https://habr.com/ru/post/1302750/


All Articles