瞧,即使 Option Explicit 处于活动状态,我也可以重新定义全局变量——但为什么呢? [英] Look Ma, I can redefine global variables even with Option Explicit active -- but why?

查看:28
本文介绍了瞧,即使 Option Explicit 处于活动状态,我也可以重新定义全局变量——但为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很棒的疯狂发现:

Here's one nice crazy discovery:

Option Explicit

ExecuteGlobal "Option Explicit: Dim TestVar: TestVar=41"
ExecuteGlobal "Option Explicit: TestVar=42"

MsgBox "TestVar=" & CStr (TestVar)

按预期工作 -- 显示 42.

works as expected -- displays 42.

还有:

Option Explicit

ExecuteGlobal "Option Explicit: TestVar: TestVar=41"

MsgBox "TestVar=" & CStr (TestVar)

在 ExecuteGlobal 调用中产生未定义变量",因为 TestVar 未定义.好的.

yields "Undefined variable" in the ExecuteGlobal call since TestVar is not defined. OK.

理解的是:

Option Explicit

ExecuteGlobal "Option Explicit: Dim TestVar: TestVar=41"
ExecuteGlobal "Option Explicit: Dim TestVar: TestVar=42"

MsgBox "TestVar=" & CStr (TestVar)

不会在第二个 ExecuteGlobal 调用中抛出Identifier redefined",但显示 42 -- 就像第二个 Dim 中的 Dimcode>ExecuteGlobal 调用不存在.

does not throw "Identifier redefined" in the second ExecuteGlobal call, but displays 42 -- as if the Dim in the second ExecuteGlobal call wasn't present.

如果你对 Class 声明做同样的事情,一切正常,即你不能在任何情况下重新定义一个类.

If you do the same with Class declarations, everything works fine, i.e. you cannot redefine a class under any circumstances.

什么鬼?

我的问题是:为什么 ExecuteGlobal 允许我重新定义全局变量,而 a) ExecuteGlobal 确实禁止访问未声明的变量,并且 b) 类定义被区别对待?

My question is: Why does ExecuteGlobal allow me to redefine a global variable while a) ExecuteGlobal does forbid access to undeclared variables and b) class definitions are treated differently?

我确实有一个用例导致了这一点(在测试运行时生成源代码并通过 ExecuteGlobal 执行它,因为某些不像你可能期望的那样奇怪),但我刚刚提出的观点是有效的没有对我认为的真实世界场景的描述就足够了.

I do have a usecase that leads to this (generating source code at test runtime and executing it via ExecuteGlobal for some not-as-weird-as-you-might-expect reason), but the points I just made are valid enough without a description of the real-world secenario I think.

我在使用 QTP(HP QuickTest Professional)时遇到过这种情况,它使用 VisualBasic 脚本主机引擎进行脚本播放,但仅在 VB 脚本主机中情况完全相同.

I came across this using QTP (HP QuickTest Professional), which uses the VisualBasic scripting host engine for script playback, but it is exactly the same situation in VB scripting host only.

推荐答案

以下代码

Option Explicit 
    ExecuteGlobal "WSCript.Echo b "

不会失败.executeglobal 的上下文不知道声明的 Option 显式.但是

will not fail. The context of the executeglobal doesn't know about the declared Option explicit. But

Option Explicit 
    ExecuteGlobal "Option Explicit : WSCript.Echo b "

因运行时错误而失败.一切都在工作,但在一个单独的环境中.还有

fails with a runtime error. Everyting is working, but in a separate environment. And

Option explicit
    Dim b
    ExecuteGlobal "Option Explicit : WScript.Echo b "

按预期工作.

在下面的代码中

Option Explicit 
    ExecuteGlobal "Option Explicit: Dim a : a = 1 : Dim a : a = 2"

您将收到重新定义的名称错误.这是一个编译器错误,而不是运行时错误.

you will get the redefined name error. And this is a compiler error, not a runtime error.

如果,如所示,你对类做同样的事情

If, as indicated, you do the same with classes

Option Explicit 
    Class thisThing
    End Class

    ExecuteGlobal "class thisThing : End Class"

您收到运行时错误,重新定义了名称.

you get a runtime error, redefined name.

因此,从您的测试和这些测试(以及更多)来看,它似乎" ExecuteGlobal 生成了一个新的上下文,在执行传递的代码时在其中工作,并且在退出时,上下文与原始调用上下文合并.

So, from your tests and these tests (and some more), it "seems" ExecuteGlobal generates a new context, work inside it while executing the passed code, and on exit, the context is merged with the original calling context.

所以,回答您的问题:

a) 如果在不同的上下文中完成,则可以重新定义"变量.合并变量的值.

a) Variables can be "redefined" if it is done in different contexts. Values of variables are merged.

b) 如果在 ExecuteGlobal 上下文中使用了显式选项,则 ExecuteGlobal 不允许访问未定义的变量.

b) ExecuteGlobal doesn't allow access to undefined variables if option explicit is used inside the ExecuteGlobal context.

c) 变量就是变量.值可以在 ExecuteGlobal 上更改,并在退出时合并.但是类重新定义正在改变某物是什么,而不是某物包含什么.

c) A variable is a variable. Value can change on ExecuteGlobal, and it is merged on exit. But class redefinition is changing what something IS, not what somenthing contains.

我没有反编译 VBScript 引擎,但这似乎与观察到的行为一致.

I've not decompiled the VBScript engine, but this seems coherent with the observed behaviour.

这篇关于瞧,即使 Option Explicit 处于活动状态,我也可以重新定义全局变量——但为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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