Nullable types in VB as Well


Eric Gunnerson

writes about C# supporting Nullable types.
We’ve heard a lot of positive feedback on the Nullable types. Just so everyone’s aware, this is NOT a C# specific feature. Just as with all generics, VB will both emit and consume the full set of generics, including Nullable types.
VB has a slightly different syntax, and is a little easier to type as the VS Whidbey Code Editor will auto expand the body:
If I have an Order object that has a DateShipped property, it would be expressed as:
Public Class Order
Public Property OrderId as Integer

Public Property DateShipped as Nullable(Of DateTime)

When the user types Nullable and hits tab, VB will automatically add “(Of “. It just reads more cleanly. Even reading C#, I would read Nullable Of DateTime. Now, C# is doing a bit of syntactical sugar and using ? in their syntax as well as Nullable
Public class Order
public int OrderId {get/set}
public DateTime? DateShipped {get/set}
It is a little strange that VB was historically criticized for supporting multiple ways to do the same thing, and confusing users which was the best. How about + &.
This becomes especially useful when working with Databases and
Some of the features we’re considering in Beta 2 are the ability to leverage Nullable(Of T) on our new TableAdapter methods. In the Beta 1 product, as well as the Community Technology Previews you’ll see methods generated insert, update, delete and fill. These have two method signatures. One strongly typed, and one with the same number of parameters that take object as their type. This was to support sending Null into the methods. Unfortunatley, this means you won’t get any compile time errors if you pass a string for a column that requires int. We are looking at moving these overloads to a single method signature:
Public Function Insert(ByVal id As Nullable(Of Integer), ByVal name As String, ByVal dateClosed As Nullable(Of DateTime)) As Integer
If dateClosed.HasValue Then
Me.InsertCommand.Parameters(0).Value = dateClosed.Value
Me.InsertCommand.Parameters(0).Value = System.Convert.DBNull
End If

Steve Lasker
Program Manager
Visual Basic Data


Leave a comment

Feedback usabilla icon