Search This Blog

Friday, March 25, 2011

Working with null values in the .NET Framework

One of the interesting aspect of application development is handling null/zero/non existing/non allocated values. We can say like "our code is expecting some value for a variable but it got nothing or some unexpected value" and this is the places where we have use proper null checking to make the system stable.


Null - The Most Common
A null value is a value that doesn't refer toany object. It's the default value of reference-type variables.
    1:  struct sTest
   2:  {
   3:      public string teststring;
   4:  }    
   5:   
   6:  sTest obj = new sTest();
   7:  if (obj.teststring == null) {
   8:  Console.WriteLine("sTest is Null")
9: }
}
This code works without any problems with C#,but,VB.NET uses the Nothing keyword. The following code demonstrate its use:
   1:  Dim sTest As String
   2:  If (sTest Is Nothing) Then
   3:  Console.WriteLine("sTest is Null")
   4:  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.
Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.
DBNull
The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it'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 null value (a null object) and an uninitialized value (the DBNull.Value instance). For example, a table can have records with uninitialized fields. By default, these uninitialized fields have the DBNullvalue.
You can utilize the DBNull object by way of its Value property. This represents a null value, which is never equal to anything. The following C# snippet revises the previous example to utilize the DBNull class:
   1:  if (sTest.Equals(System.DBNull.Value)) {
   2:  Console.WriteLine("sTest is null.");

same code, like this:
If (sTest.Equals(System.DBNull.Value) Then
Console.WriteLine("sTest is Null")
End If
You may also use the DBNull.Value constant on the right-hand of an assignment operation, thus storing a null value 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 type contains a null value. The following VB.NET uses IsDBNull to checkan object's value:
   1:  Dim oTest As Object = DBNull.Value
   2:  If ((IsDBNull(oTest))) Then
   3:  Console.WriteLine("oTest is Null")
   4:  End If
Even if you check for null values at every conceivable location within your code, they still may creep into the application. So you have to provide the try/catch exception block; it allows you to catch any unhandled exceptions that null values cause. The System namespace provides the NullReferenceException for these situations.
   1:  try {
   2:   
   3:  if (sTest.Equals(System.DBNull.Value)) {
   4:  Console.WriteLine("sTest is null.");
   5:  }
   6:   
   7:  } catch (System.NullReferenceException e) {
   8:  Console.WriteLine("Error: " + e.ToString());
9: }Handle your data with care
So now you have a uniform mechanism for checking any uninitialized references in your application.......

No comments:

Post a Comment