Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

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

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 variable or a literal. Can someone explain this behavior to me, why do VBScript acts this way ?

Here is a sample 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 ouputs :

Long variable = String variable : Not Equal

Long constant = String variable : Not Equal

Long constant = Long variable : Equal

Long variable = String literal : Equal

Long constant = String literal : Equal

Which is quite confusing o_O

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...