Vbscript 有没有办法取消对变量的定义?
我只是想知道是否有可能取消对变量的调暗 假设这是我在ASP页面中使用的#include文件Vbscript 有没有办法取消对变量的定义?,vbscript,asp-classic,Vbscript,Asp Classic,我只是想知道是否有可能取消对变量的调暗 假设这是我在ASP页面中使用的#include文件 Dim MyVar MyVar = "Hello World" Response.write(MyVar) 'From now on I can not use Dim MyVar anymore as it throws an error 'I have tried MyVar = Nothing Set MyVar = Nothing 'But again, when you do Dim MyVar
Dim MyVar
MyVar = "Hello World"
Response.write(MyVar)
'From now on I can not use Dim MyVar anymore as it throws an error
'I have tried
MyVar = Nothing
Set MyVar = Nothing
'But again, when you do
Dim MyVar
'It throws an error.
原因是我不能每页多次使用同一个#INCLUDE文件。是的,我确实喜欢使用optionexplicit,因为它可以帮助我保持代码的整洁
*)编辑:我发现它没有我想要的那么清楚
假设这是一个“include.asp”
现在进入asp页面:
<!--#include file="include.asp"-->
<%
'Now: I do not see whats in include above and I want to use variable A
Dim A
'And I get an error
'I also cannot use the same include again:
%>
<!--#include file="include.asp"-->
<!--#include file="include.asp"-->
<%
Dim A
A=1
SetCookie "Beer", A
A=1 ' This is kind of redundant in this code.
SetCookie "Beer", A
%>
<!--#include file="globals.asp"-->
<!--#include file="include.asp"-->
<%
Dim A
A=....something......
%>
<!--#include file="include.asp"-->
不,没有办法“取消”变量。幸运的是,你也不需要这个
每当您试图在同一范围内两次声明变量时,您已经犯了错误。认为运行时不允许您这样做是有帮助的。
解决方案:
- 不要使用全局变量。使用函数,在那里声明变量
- 不要多次包含同一文件
我同意Tomalak的观点-我真的不知道为什么您需要(或想要)包含同一个文件两次(?)
这似乎是一种糟糕的设计理念,最好将例程封装在include文件中,作为可以调用的函数或子例程——无需包含两次
此外,虽然你不能取消身份验证,但你可以重新身份验证,但鉴于你似乎想做的事情,我不想鼓励不良做法
你可以用这样的东西来代替所有的东西:
Include.asp:
<%
Function SetCookie(scVar, scVal)
Response.cookie (scVar) = scVal
End Function
%>
<%
globalVarA=1
Response.Cookie("beer")=globalVarA
%>
asp页面:
<!--#include file="include.asp"-->
<%
'Now: I do not see whats in include above and I want to use variable A
Dim A
'And I get an error
'I also cannot use the same include again:
%>
<!--#include file="include.asp"-->
<!--#include file="include.asp"-->
<%
Dim A
A=1
SetCookie "Beer", A
A=1 ' This is kind of redundant in this code.
SetCookie "Beer", A
%>
<!--#include file="globals.asp"-->
<!--#include file="include.asp"-->
<%
Dim A
A=....something......
%>
<!--#include file="include.asp"-->
我建议您不要尝试这样做,因为这会在同一范围内赋予MyVar不同的含义。为什么不在将MyVar设置为Nothing之后重用它呢。e、 g.MyVar=“Hello World2”
?OP很可能试图使用ASP包含文件,如IIS服务器端包含(SSI
)。这仍然是错误的,但这可以解释为什么他不止一次地把事情包括在内。在模块中显式地声明变量Private
或Public
,这样做的意图比使用Dim
要清楚得多。4.如果要将状态保持在函数之外,请使用类来创建对象。@AutomatedChaosPrivate
和Public
仅在VBScript中的类中工作。没有模块,这是VBA。如果您使用Windows脚本文件
(.WSF)或HTML应用程序
(.HTA),则包括
之类的脚本。我称之为模块,这可能不是正确的术语,但是private
和public
的工作方式与您从private和public声明中所期望的一样;不仅仅是在课堂上。@AutomatedChaos啊,那很好。。。直到现在我才知道。所以我想ASP的
也是如此。