在具有经典ASP的VB中,如果变量名称相似,则局部变量将隐藏全局声明

时间:2014-05-28 09:27:18

标签: vbscript asp-classic

我正在研究经典ASP,并且我已将变量abc声明为全局变量,并且我还在函数范围内声明另一个变量vb中的同一变量abc将局部变量隐藏全局变量声明?

以下是我声明局部变量的方式:

Sub TestFunction()
  dim abc
  abc = GetItem()
End Sub

2 个答案:

答案 0 :(得分:0)

简短回答是

<%
dim test
test = "hello"

sub tryme
  response.write test & " - 1<br>"
  dim test
  response.write test & " - 2<br>"
  test = "hello2"
  response.write test & " - 3<br>"
end sub


  response.write test & " - 0<br>"
call tryme
  response.write test & " - 4<br>"
%>

输出:

hello - 0
- 1
- 2
hello2 - 3
hello - 4

答案 1 :(得分:0)

答案是肯定的,举个例子;

'Global Scope
Dim abc
abc = 1

Call Response.Write(Test1() & "<br />") 'Will output 2
Call Response.Write(Test2() & "<br />") 'Will output 1

'Change global variable value.
abc = 2

'Local variable abc will not be affected by the global variable value change.
Call Response.Write(Test1() & "<br />") 'Will output 2
Call Response.Write(Test2() & "<br />") 'Will output 3

Function Test1()
  'Local Scope
  Dim abc
  abc = 2
  Test1 = abc
End Function

Function Test2()
  Test2 = abc
End Function

输出将是;

2
1
2
3

任何与全局变量名称匹配的本地声明的变量都将采用先例。它们无法连接,更改局部变量abc永远不会影响全局变量abc的值。


有用的链接