在ASP中判断一个对象是否已被释放

<%
Dim conn '声明
Set conn = Server.CreateObject("ADODB.Connection") '创建
'
使用
Set conn = Nothing '释放
%>

我们通常用如上的形式来创建一个对象,并使用和释放它,问题是我们怎么去判断一个对象是否已被释放了呢?用IsObject可以吗?我们来试下:

<%
Dim conn
Response.Write(
IsObject(conn)) '结果为False
Set conn = Server.CreateObject("ADODB.Connection")
Response.Write(
IsObject(conn)) '结果为True
Set conn = Nothing
Response.Write(
IsObject(conn)) '结果为True
%>

可见并不能使用IsObject来判断一个对象是否已被释放,那我们用VarType或TypeName函数来试试看:

<%
Dim conn
Response.Write(
TypeName(conn)) '结果Empty
Set conn = Server.CreateObject("ADODB.Connection")
Response.Write(
TypeName(conn)) '结果Connection
Set conn = Nothing
Response.Write(
TypeName(conn)) '结果Nothing
%>

所以,判断一个对象是否被释放我们应用:TypeName(conn) = "Nothing"
注意:一定要用Nothing不能用nothing,小写结果就不为True了。

原文地址:https://www.cnblogs.com/wintalen/p/2082480.html