Link Round-Up for 9/27/07How To Pass Data Between Forms in VB.NET

Introduction to Nullable Types in VB.NET

September 27th, 2007

Yeah, well, sometimes nothin' can be a real cool hand.In .NET nullable data types are data types that can be set to a null reference or, in VB.NET terms, Nothing. They can either contain a value or have no value at all. Reference data types, like strings and classes, are nullable. However, Value types, such as integers and dates, are not nullable. If you set a value type to Nothing the result is a default value such as 0 for numeric types or #1/1/0001 12:00:00 AM# for dates.

As you can probably guess, there may be times when you will need to have value data types that can be Null/Nothing. For example, a database column of date values could be Null in the database and you want your class to reflect this situation. In the .NET Framework 2.0, we have the Nullable class and the Nullable structure that allows us to add the ability to have a value of Nothing to an underlying value type.

In practice, we will want to use the Nullable structure since it supports Generics and thus allows you to define a specific data type. This gives you greater type safety and it’s easier to work with.

To start things off, let’s create a fragment of a class that uses a Nullable structure as a property.

Public Class Orders
    '
    ' Other property variables
    '
    Protected _orderShippedOn As Nullable(Of Date) 

    '
    ' Other methods and properties
    '

    Public Property OrderShippedOn() As Nullable(Of Date)
        Get
            Return _orderShippedOn
        End Get
        Set(ByVal value As Nullable(Of Date))
            _orderShippedOn = value
        End Set
    End Property
End Class

In this example, we have an order shipped on date value. Since we will want the order in the system prior to shipping it out the door, the value in our database will be Null until this event happens. We don’t want to hard code values like this in several places in our application…

If CurrentOrder.OrderShipDate = #12:00:00 AM# Then

…to fake out null values. Instead, we wrapper our dates in a Nullable class.

HasValue and Value

But, you’ll notice that if you code a line like this…

 
If CurrentOrder.OrderShippedOn Is Nothing Then

…we’ll get an error. This is because we’re still dealing with a structure, not a class. Here’s what our code should look like:

If Not CurrentOrder.OrderShippedOn.HasValue Then
    dtpOrderShipDate.Value = Now
Else
    dtpOrderShipDate.Value = CurrentOrder.OrderShippedOn.Value
End If

The HasValue property returns a Boolean value that lets us know if there is a value or not. Then, when we want to retrieve the value to load it into a Date Time Picker control, we use the Value property. This allows us to return the correct type for the control. You can’t use = or Is to compare a Nullable type, you have to use the HasValue or Value properties.

Loading Nulls from the Database

Now let’s look at how we should load our object from our database. In this example, we’re assuming that we’re loading our object from a DataRow.

If orderDataRow.Item("ShipDate") Is DBNull.Value Then
    _orderShippedOn = Nothing
Else
    _orderShippedOn = CType(orderDataRow.Item("InventoryDate"), Date)
End If

We have to check for a Null value since we can’t convert a DBNull to a Date.

That’s it for this introduction to Nullable types. I hope you’ve found it useful. Let me know if you have any questions, additions, or corrections by leaving me a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (13 votes, average: 4.69 out of 5)
Loading ... Loading ...

5 Comments Add your own

  • 1. Sami  |  March 25th, 2008 at 11:28 pm

    nice expalanation

  • 2. Kris Kraus  |  June 5th, 2008 at 3:26 pm

    Good job. Thanks.

  • 3. hobbylobby  |  July 27th, 2008 at 8:10 am

    you show getting values from a database, but not setting values to a database.

    here’s one way:

    If theDate.HasValue Then
    cmd.Parameters.Add( _
    New OleDb.OleDbParameter(”@theDate”, theDate))
    Else
    cmd.Parameters.Add( _
    New OleDb.OleDbParameter(”@theDate”, DBNull.Value))
    End If

  • 4. Matt  |  October 2nd, 2009 at 4:49 pm

    Very nice, ive been using Strings to hold my nullable date columns. Not very elegant but it did the trick. I can now go back and fix all of those.

    Thank you very much!

  • 5. AussiePete  |  February 13th, 2010 at 10:01 pm

    Thanks Guys,

    I’ve been struggling with this for a day and half !!

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives