Let's
take a closer look at working with nullvalues within the .NET Framework.
Null and void
A
null value is a value that doesn't refer toany object. It's the default value
of reference-type variables(e.g., class, interface, delegate).
These
items are called reference types becausethey're objects that store references
to the actual data. String,object, and struct are reference type examples. The
null keyword isa literal that represents a null reference with the C#
.NETenvironment. You may use the null keyword to check or assign thevalue of an
object. For example, the following C# code determinesif a string value is null:
string sTest = "Test";
if (sTest == null) {
Console.WriteLine("sTest is Null")
}
This
code works without any problems with C#,but there's no VB.NET equivalent of the
null keyword. Instead,VB.NET uses the Nothing keyword. The following code
demonstratesits use:
Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine("sTest is Null")
End If
Another
area of confusion stems from the factthat VB.NET doesn't treat null and Nothing
as equals; consequently,a VB.NET programmer may have to check for both values.
The
variation in support may cause confusion,but a developer rarely develops in
both languages simultaneously.On the other hand, Microsoft provides a uniform
method for workingwith null values: The base System.Convert namespace includes
theDBNull object.
DBNull
The
use of the DBNull class indicates theabsence of a known value. While the
Microsoft documentation saysit's typically in a database application, you may
also use it withany type of data.
In
database applications, a null object is avalid value for a field. This class
differentiates between a nullvalue (a null object) and an uninitialized value
(the DBNull.Valueinstance). For example, a table can have records with
uninitializedfields. By default, these uninitialized fields have the
DBNullvalue.
You
can utilize the DBNull object by way of itsValue property. This represents a
null value, which is never equalto anything. The following C# snippet revises
the previous exampleto utilize the DBNull class:
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");
Notice
how this code utilizes the stringclass's equality method. The VB.NET sample may
utilize the samecode, like this:
If (sTest.Equals(System.DBNull.Value)
Then
Console.WriteLine("sTest is Null")
End If
You
may also use the DBNull.Value constant onthe right-hand of an assignment
operation, thus storing a nullvalue within a reference type. In addition, the
.NET Frameworkprovides the IsDBNull function (in the System.Convert
namespace),which is a shorthand approach for determining if a reference
typecontains a null value. The following VB.NET uses IsDBNull to checkan
object's value:
Dim oTest As Object = DBNull.Value
If ((IsDBNull(oTest))) Then
Console.WriteLine("oTest is Null")
End If
Even
if you check for null values at everyconceivable location within your code,
they still may creep intothe application. Fortunately, the try/catch exception
blockprovides another line of defense; it allows you to catch anyunhandled
exceptions that null values cause. The System namespaceprovides the
NullReferenceException for these situations. See howthe following C# code uses
NullReferenceException.
try {
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");
}