Visual Basic 6.0 Upgrading ...


Visual Basic Reference: Error Messages
Behavior of Null has changed

In Visual Basic 6.0, the Null keyword indicated that a variable contained no valid data, and the IsNull function was used to test for Null. In Visual Basic .NET, the Null keyword is still a reserved word, but it has no syntactical value; the IsNull function is no longer supported. In addition, Visual Basic 6.0 supported Null propagation — when Null was used in an expression, the result of the expression would also be Null. Null propagation is no longer supported in Visual Basic .NET.

During upgrade, Null is converted to DBNull, and IsNull is converted to IsDBNull. The behavior of DBNull is slightly different than that of Null. Null could be used in functions and assignments; DBNull cannot.

Where Null was used with a Variant data type, the Variant is converted to Object during upgrade; it may be more appropriate to use the Nothing keyword or the IsNothing function.

What to do next

  • Review the use of Null in your code. In some cases, functions that worked with Null will cause an exception with DBNull; you will need to test for DBNull in these cases:
    ' Visual Basic 6.0
    ' Assumes that rs!Field1 is null.
    x = Left(rs!Field1, 1)
    
    ' After upgrade to Visual Basic .NET
    x = VB.Left(rs.Fields("Field1").Value, 1)
    
    ' Modified code
    If Not IsDbNull rs.Fields("Field1").Value Then
       x = VB.Left(rs.Fields("Field1").Value, 1)
    End If

    In other cases, code can be simplified:

    ' Visual Basic 6.0 code
    If Not IsNull (rs!Field1, 1) Then
       x = rs!Field1
    End If
    
    ' After upgrade to Visual Basic .NET
    x = VB.Left(rs.Fields("Field1").Value, 1)
    
    ' Modified code.
    ' UPGRADE_WARNING: Use of Null/IsNull() detected.
    If Not IsDbNull(rs.Fields("Field1").Value) Then
       x = rs.Field1("Field1").Value
    End If
    
    ' Modified code – If statement no longer needed.
    x = rs.Field1("Field1").Value & " "

See Also

IsDbNull Function | DBNull Class

Page view tracker