Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 21 / 68

Writing null-safe code (the If operator)

This lesson covers handling values safely against Nothing in VB.NET, with the aim of preventing errors when a value has not been set.

VB.NET represents "no value has been set" with Nothing. Writing If(value, alternative) supplies a substitute when the value is Nothing, all on one line.

The sample code displays a substitute string with If(address, "No address set") when address is Nothing, while a name that has been set is used as it is.

A common early stumble is using a value that is still Nothing and hitting an error. Using a possibly-Nothing value without checking causes a run-time error that stops the program.

In professional work, allowing for values from a database or an API being empty, and writing null-safe code, is treated as important for preventing errors.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)