如何检查对象是否已设置?

时间:2016-11-16 11:29:13

标签: vbscript

我有:

'Set logFileObj = fso.CreateTextFile("cit.log",True)    
if logFileObj = "" then 
    WScript.StdOut.WriteLine "logFileObj not set"
else
    WScript.StdOut.WriteLine "logFileObj set"
end if

这很有效。然后我尝试:

Set logFileObj = fso.CreateTextFile("cit.log",True)    
if logFileObj = nothing then 
    WScript.StdOut.WriteLine "logFileObj not set"
else
    WScript.StdOut.WriteLine "logFileObj set"
end if

我得到Microsoft VBScript runtime error: Object doesn't support this property or method

我已将if logFileObj = "" then替换为if logFileObj = nothing then

2 个答案:

答案 0 :(得分:1)

要与Nothing进行比较,您必须使用Is而不是=

Set logFileObj = fso.CreateTextFile("cit.log", True)
If logFileObj Is Nothing Then
    WScript.StdOut.WriteLine "logFileObj not set"
Else
    WScript.StdOut.WriteLine "logFileObj set"
End If

这是因为=不能用于比较VBScript中的对象引用,Nothing是对空对象的引用。

有关详细信息,请参阅Nothing上的文档:https://msdn.microsoft.com/en-us/library/f8tbc79x(v=vs.84).aspx

答案 1 :(得分:-2)

您可以使用Isobject,如下面的代码所示: -

if isobject(logFileObj) then
   WScript.StdOut.WriteLine "logFileObj not set"
else    
   WScript.StdOut.WriteLine "logFileObj set"
end if