VBScript 函数中的变量范围 [英] Variable scope in VBScript functions
问题描述
我有一个关于 VBScript 中变量作用域的问题.我知道有以下关键字(来自 autoitscript.com):>
I have a question about variable scope in VBScript. I know there's the following keywords (from autoitscript.com):
- Dim = 局部作用域,如果变量名在全局不存在(在这种情况下它重用全局变量!)
- Global = 强制在全局范围内创建变量
- Local = 强制在 Local/Function 范围内创建变量
假设我有以下 .vbs 文件:
Imagine that I have the following .vbs file:
Dim strPath
strPath = "C:\folder"
DisplayPath strPath
Sub DisplayPath(strPath) 'Does this strPath get it's own local scope?
MsgBox strPath
End Sub
在函数:DisplayPath(strPath)
中,strPath
是局部变量吗?或者函数/子函数是否可以访问在脚本主要部分顶部定义的 strPath
作为全局变量?
In the function: DisplayPath(strPath)
, is strPath
a local variable? Or do functions/subs have access to the strPath
defined at the top of the main section of the script as a global variable?
此外,明确使用 Dim
与仅在我使用变量时定义变量有什么意义,这在脚本语言中是可能的?
Also, what's the point of explicitly using Dim
versus just defining variables as I use them, which is possible in scripting languages?
推荐答案
DisplayPath
过程中的 strPath
将是一个新变量,但不是出于您预期的原因,您的代码存在细微的问题,会使问题变得模糊.
The strPath
in the DisplayPath
procedure will be a new variable but not for the reasons you expect, there is subtle problem with your code that will cloud the issue.
当调用 Sub
过程时,VBScript 语法不包括括号.例如:-
When calling Sub
procedure the VBScript syntax does not include parentheses. For example:-
Sub MyProc(Param1, Param2)
'' # Do stuff
End Sub
MyProc("Hello", "World")
以上会导致语法错误.它应该被称为:-
the above would result in a syntax error. It should be called:-
MyProc "Hello", "World"
现在当只有一个参数时不会发生语法错误.这是因为括号的另一种用法是作为表达式的一部分,例如'(a + b) * c'.在以下情况下:-
Now when there is only one parameter a syntax error does not occur. This is because another use of parentheses is as part of an expression e.g. '(a + b) * c'. In the case of:-
DisplayPath(strPath)
VBScript 解析表达式"(strPath)
并将结果 传递给DisplayPath
.产生新存储的这个结果保存了结果表达式.
VBScript resolves the "expression" (strPath)
and pass the result to DisplayPath
. Its this result that gives rise to new storage hold the result the expression.
你打电话了吗
DisplayPath strPath
没有新建.
但是这又如何:-
Sub DisplayPath(something)
MsgBox something
End Sub
仍然没有分配新的存储空间.something
将指向与 strPath
相同的内存.
There is still no new storage allocated. something
will point at the same memory that strPath
does.
编辑
以下代码有效:-
Dim strPath
strPath = "c:\folder"
Display
Sub Display()
MsgBox strPath
End Sub
在过程之外声明 strPath
使其具有全局作用域.
The declaration of strPath
outside of a procedure causes it to have global scope.
至于使用显式Dim
如果上面的赋值行看起来像这样会发生什么?
As to the point of using explicit Dim
what would happen if the assignment line above looked like this?
strPath = "c:\folder"
一个名为 strPath
的新变量将出现,而 strPath
将保持为空.您应该总是以以下行开始您的 VBScript 文件:-
A new variable called strPath
would come into existence and strPath
would remain empty. You should always begin your VBScript files with the line:-
Option Explicit
这将强制您显式Dim
要使用的所有变量,并为您节省数小时的调试时间.
This will force you to explicitly Dim
all variables to be used and will save you hours of debugging time.
这篇关于VBScript 函数中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!