IF语句中的VBScript隐式转换(从变量到文字)是否不同? [英] VBScript implicit conversion in IF statement different from variable to literals?

查看:67
本文介绍了IF语句中的VBScript隐式转换(从变量到文字)是否不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于VBScript(经典ASP)中的IF语句中的隐式转换,在处理变量或文字时,隐式转换与隐式转换的方式不同,因此我们当前遇到了问题.有人可以向我解释这种行为吗,为什么VBScript会这样做?

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

哪些产出:

长变量=字符串变量:不相等

Long variable = String variable : Not Equal

长常量=字符串变量:不相等

Long constant = String variable : Not Equal

长常量=长变量:等于

长变量=字符串文字:等于

Long variable = String literal : Equal

长常量=字符串文字:等于

Long constant = String literal : Equal

o_O哪个让人很困惑

Which is quite confusing o_O

推荐答案

您(隐式地)声明了变量As Variant,因此您的If条件实际上测试了两个Variant的相等性,并确定它们是不相等的

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.

但是,在最后一种情况下,您将使用String常量(即使没有类型也不能使用Variant常量)和String常量.

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

我的猜测是,当您比较两个Variant时,VB首先确定它们是否具有相同的类型标签,如果没有,则解析为False.

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.

这篇关于IF语句中的VBScript隐式转换(从变量到文字)是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆