Upgrade Recommendation: Avoid Null Propagation

Previous versions of Visual Basic support 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 2008. The statement 1+Null will generate a type mismatch in Visual Basic 2008. 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 2008 only has one version, Left, which always returns a string.

Note

Starting with Visual Basic 2005, a form of null propagation is supported for types declared as Nullable. This does not change the recommendation to avoid null propagation in applications that are being upgraded.

In order to be compatible with both Visual Basic 6.0 and Visual Basic 2008, you should always write code to test for Null instead of relying on Null propagation. Furthermore, in Visual Basic 2008, 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

Other Resources

Language Recommendations for Upgrading