VBScript:如何判断不同子类型变量是否为空(未被初始化)

普通子类型变量

如果普通子类型变量未被初始化,它的值为Empty。

'将变量变成未初始化
varTest = Empty
 
'Chenk检查变量是否为空,方法一
If varTest = Empty Then
        MsgBox "The variable is Empty."
End If
 
'Chenk检查变量是否为空,方法二
If IsEmpty(varTest) Then
        MsgBox "The variable is Empty."
End If

对象(Object)子类型变量

如果对象变量中的对象被销毁或是该变量还没有初始化时,它就等于一个子类型为Object,值为Nothing的变量。Nothing值类似于Null值。不能像对待普通的变量那样用等号(=)检查对象变量的值是否是Nothing。而是要用专门的操作符Is。而销毁对象时,必须要同时使用Set关键字和等号。

Dim objFSO
Dim boolExists
 
'创建一个对象子类型变量
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
 
If IsObject(objFSO) Then
    boolExists = objFSO.FileExists("C:\autoexec.bat")
    MsgBox boolExists
    
    '销毁该对象子类型变量
    Set objFSO = Nothing
    '判断对象变量的值是否为Nothing
    If objFSO Is Nothing Then
        MsgBox "The object has been destroyed, which frees up the resources it was using."
    End If
End If
原文地址:https://www.cnblogs.com/ITGirlXiaoXiao/p/3134001.html