Search code examples
objectvbscript

Check if object is set in VBScript


Assume I create an object:

set newcon = Server.CreateObject("ADODB.Connection")

And at some point I destroy it:

set newcon = nothing

How can I tell if newcon is an object or nothing?

I have tried:

newcon is nothing

but I get object required.

If I try isobject or isnull or isempty it doesn't return either true or false.

Is there something else that really works?


Solution

  • I believe you accidentially changed newcon before testing it with Is Nothing:

    >> set newcon = CreateObject("ADODB.Connection")
    >> WScript.Echo 0, IsObject(newcon)
    >>
    0 -1
    >> set newcon = Nothing
    >> WScript.Echo 1, IsObject(newcon)
    >>
    1 -1
    >> WScript.Echo 2, newcon Is Nothing
    >>
    2 -1
    >> newcon = "oops"
    >> WScript.Echo 3, newcon Is Nothing
    >>
    Error Number:       424
    Error Description:  Object required
    >> WScript.Echo 4, newconn Is Nothing ' oops
    >>
    Error Number:       424
    Error Description:  Object required
    

    If you get Error 434, then newcon (or the variable you really test) doesn't hold an object or Nothing.