Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Null or Nothing from VBScript function?

Tags:

vbscript

I'm trying to write the VBScript equivalent of a function similar to what's below:

object getObject(str)
{
    if ( ... )
    {
        return object_goes_here;
    }

    return null;
}

My guess would be below, except that I'm not understanding the difference between Nothing and Null. As a caller, I'd rather test if the return value is set by using IsNull() versus X Is Nothing.

Function getObject(str)
    If ... Then
        Set getObject = object_goes_here
        Exit Function
    End If

    Set getObject = Nothing  // <-- or should this be Null?
End Function
like image 236
user1128144 Avatar asked Jan 20 '12 15:01

user1128144


1 Answers

In your sample code, the object gets always Nothing because that is the last action. This is how it should be:

Function getObject(str)
     If ... Then
         Set getObject = object_goes_here
         Exit Function
     End If
     Set getObject = Nothing
End Function

or:

Function getObject(str)
     Set getObject = Nothing  
     If ... Then
         Set getObject = object_goes_here
     End If
End Function

The answer of GSerg is correct: you should use Nothing. Additionally, to see if an object has a null reference, use:

If Not object Is Nothing Then
    '  do something
End If
like image 87
AutomatedChaos Avatar answered Sep 22 '22 23:09

AutomatedChaos