测试对象是否为Nothing会导致"需要对象"错误

Eas*_*ere 8 vbscript asp-classic

我支持一些经典的ASP页面,其中一个使用并重新使用对象conn并在.asp页面完成处理时或在页面重定向到另一个页面之前处理它.

<%
dim conn
...
set conn = server.CreateObject("adodb.connection")
...
sub cleanUp()
    conn.Close
    set conn = nothing
end sub
...
sub pageRedirect(url)
    call cleanUp()
    response.Redirect url : response.End
end sub
...
' very end of file
call cleanUp()%>
Run Code Online (Sandbox Code Playgroud)

我发现如果有重定向,我就会在该行发生服务器错误conn.Close,需要Microsoft VBScript运行时错误'800a01a8'对象.我认为没有理由为什么那行会执行多次但是为了安全我重写了这个函数

sub cleanUp()
    if(not (conn Is Nothing)) then
        conn.Close
        set conn = Nothing
    end if
end sub
Run Code Online (Sandbox Code Playgroud)

但我仍然得到那个确切的错误,现在就行if(not (conn Is Nothing))!我认为目的Is Nothing是在使用变量名称conn之前进行测试,以防止"对象需要"错误,但测试会抛出相同的错误.

如果conn已被设置为Nothing,我可以使用什么其他测试来确保conn不被引用?

Ale*_* K. 15

is nothing用于测试对象引用,如果变量不包含这样,那么测试无效并引发错误,因此conn只能在它set到达之后进行测试.

您可以;

if isobject(conn) then
   if not (conn Is Nothing) then set conn = nothing
end if
Run Code Online (Sandbox Code Playgroud)