Share via


IsMissing function was changed to IsNothing

In previous versions of Visual Basic the IsMissing function was used to test for optional arguments passed to user-defined function procedures as variants. Default values were not required for optional arguments; if no value was passed then IsMissing returned true. It was also possible to specify a default value of a primitive type to pass to the Variant argument.

In Visual Basic 2008, the IsMissing function has been removed from the language, and default values are now required for all optional arguments. During upgrade, variants are replaced with objects, and an object's default value of Nothing is added to all optional arguments.

The following code illustrates the upgrade of code that uses the IsMissing function:

' Visual Basic 6.0
Function DoThis(Optional X As Variant)
   If IsMissing(X) Then
      ' Do something here.
   End If
End Function

' After upgrade to Visual Basic 2008
Function DoThis(Optional ByRef X As Object = Nothing) As Object
   ' UPGRADE_WARNING: IsMissing function was changed to IsNothing.
   If IsNothing(X) Then
      ' Do something here.
   End If
End Function

In most cases this code should execute properly; however, in some cases it may not give the expected result. If X in the above example is an object being passed as a variant and the object has been set to Nothing, in Visual Basic 6.0 IsMissing would have returned false. In Visual Basic 2008, the value of X would be Nothing and IsNothing would return true.

What to do next

  1. Search for any code that calls this function. Step through the function call and inspect the value of the optional arguments that are being passed.

  2. If a value is being passed and IsNothing returns true, modify your code to add an additional test for IsNothing in the calling procedure:

    ' Visual Basic 6.0
    Dim X As Object
    ' Do something with the object.
    Set X = Nothing
    ' IsMissing in the DoThis function returns false.
    DoThis(X)
    
    ' Visual Basic 2008
    Dim X As Object
    ' Do something with the object.
    X = Nothing
    If Not IsNothing(X) Then
       DoThis(X)
    End If
    

See Also

Concepts

Procedure Declaration for Visual Basic 6.0 Users

Reference

IsNothing Function