Export (0) Print
Expand All

Upgrade Recommendation: Avoid Null Propagation

Previous versions of Visual Basic supported Null propagation. Null propagation supports the premise that when Null is used in an expression, the result of the expression will itself be Null. In each case in the following example, the result of V is always Null.

Dim V
V = 1 + Null
V = Null + Right$("SomeText", 1)
V = Right("SomeText", 0)

Null propagation is not supported in Visual Basic .NET. The statement 1+Null will generate a type mismatch in Visual Basic .NET. Additionally, where Visual Basic 6.0 had two versions of the Left function — Left$ returning a string, Left returning a variant which could be Null — Visual Basic .NET only has one version, Left, which always returns a string.

In order to be compatible with both Visual Basic 6.0 and Visual Basic .NET, you should always write code to test for Null instead of relying on Null propagation. Furthermore, in Visual Basic .NET, the following functions will no longer return Null:

Chr, Command, CurDir, Date, Environ, Error, Hex, LCase, LTrim, Oct, Right, RTrim, Space, Str, Time, Trim, UCase.

Null propagation is commonly used in database applications, where you need to check if a database field contains Null. In these cases you should check results using the function IsNull() and perform the appropriate action.

See Also

Language Recommendations for Upgrading

© 2015 Microsoft