Nullable types in VB as Well

Published 28 May 04 08:08 AM
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 ADO.net
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
Else
Me.InsertCommand.Parameters(0).Value = System.Convert.DBNull
End If


Steve Lasker
Program Manager
Visual Basic Data
by VBTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# rbishop said on May 28, 2004 11:16 AM:
good work. let's see more technical posts on the vb team blog.
# Tom said on May 28, 2004 2:24 PM:
Great!
# Fan said on May 29, 2004 4:06 AM:
In C#, variables of nullable types can use the operators defined in the origin types.
For example:

int? i = 100;
i = i + 20;

I found VB doesn't allow this operation. Is there any plan to add this feature? Or else I have to code like this?

Dim i As Nullable(Of Integer) = 100
i = New Nullable(Of Integer)(i.Value + 20)
# Panopticon Central said on June 4, 2004 1:14 PM:
Nothing doing.
# Code/Tea/Etc... said on June 8, 2004 12:34 AM:
# Eric Newton said on June 17, 2004 10:39 PM:
ah hold on a sec... the ADO.Net parameter types wont auto convert nullable types to DBNull.Value?

<code>
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
Else
Me.InsertCommand.Parameters(0).Value = System.Convert.DBNull
End If
</code>
should read (in my opinion):
<code>
Public Function Insert(ByVal id As Nullable(Of Integer), ByVal name As String, ByVal dateClosed As Nullable(Of DateTime)) As Integer
Me.InsertCommand.Parameters(0).Value = dateClosed
</code>
# Steve Lasker said on June 18, 2004 1:57 PM:
Hi Eric, the timing is great.
Adding this logic directly to the Parameter object is something we've considered, and would like to do. If we don't get the parameter work done, we are looking at adding this conversion logic to the TableAdapter methods we generate in the new DataSet Designer. This is something we're making a decision on this week.
What is your opinion of exposing method signatures with Nullable(Of Integer) compared to signatures with Object as the parameter type.
In either case, we would do the conversion to DBNull. However, exposing Object doesn't help the user understand the type, or give compile time verification. The concern is VB users will be lost if they see System.Nullalbe(Of Integer) in the intellisense.
This following is a section of the spec we're considering:
Null to DBNull ConversionUnfortunately the Parameter object is unaware of Nullable(Of T) so we can’t just pass the parameter passed in to the method directly to the Parameter object. We must do a conversion of Nullable(Of T) to Null/Nothing.
For each parameter that was declared as Nullable, we’ll generate the DBNull conversion. For each Ref type, we’ll also check for null and do the conversion For each column that was specified as AllowDBNull = false, throw an ArguementNullException with the name of the specified column. Note, we are not adding text to the exception to minimize localization. For each parameter that supports Nullable(Of T), we’ll generate the following code:
If Original_CustomerID.HasValue Then
Me.DeleteCommand.Parameters(0).Value = Original_CustomerID.Value
Else
Me.DeleteCommand.Parameters(0).Value = System.Convert.DBNull
End If
Languages that do not support generics will simply follow the next scenario and compare to nothing
If the parameter doesn’t support Nullable(Of T), either because it’s a reference type, or because AllowDBNull = False, we’ll generate the following code:
If Original_CustomerName = Nothing Then
Me.DeleteCommand.Parameters(0).Value = System.Convert.DBNull
Else
Me.DeleteCommand.Parameters(0).Value = Original_CustomerName
End If
For any column, of refererence type, that was specified as AllowDBNull = False, we’ll throw an ArgumentNullException when the parameter is null/nothing.
If DateAccountOpened = Nothing Then
Throw New ArgumentNullException("DateAccountOpened")
Else
Me.InsertCommand.Parameters(4).Value = DateAccountOpened
End If
An example would look like the following:
Column
DataType
AllowDBNull

CustomerId
Int
False

CustomerName
String
False

ContactName
String
True

DateAccountClosed
DateTime
True

DateAccountOpened
DateTime
False


Public Function Insert(CustomerId As Integer, CustomerName As String, ContactName As String, DateAccountOpened As DateTime, DateAccountClosed As Nullable(Of DateTime)) As Integer
If CustomerId = Nothing Then
Throw New ArgumentNullException("CustomerId")
Else
Me.InsertCommand.Parameters(0).Value = CustomerId
End If
If CustomerName = Nothing Then
Throw New ArgumentNullException("CustomerName")
Else
Me.InsertCommand.Parameters(1).Value = CustomerName
End If
If ContactName = Nothing Then
Me.InsertCommand.Parameters(2).Value = System.Convert.DBNull
Else
Me.InsertCommand.Parameters(2).Value = ContactName
End If
If DateAccountOpened = Nothing Then
Throw New ArgumentNullException("DateAccountOpened")
Else
Me.InsertCommand.Parameters(4).Value = DateAccountOpened
End If
If DateAccountClosed.HasValue Then
Me.InsertCommand.Parameters(3).Value = DateAccountClosed.Value
Else
Me.InsertCommand.Parameters(3).Value = System.Convert.DBNull
End If
Try
Me.InsertCommand.Connection.Open()
Return Me.InsertCommand.ExecuteNonQuery
Finally
Me.InsertCommand.Connection.Close()
End Try
End Function



# Brian Russell said on June 25, 2004 2:53 PM:
Ok, this is all well and good, but what do I do in the mean time? How do I create a strongly typed object that represents a database table structure? (Please don't say strong typed datasets, not where we are going). I need a way...now...to expose an integer as a nullable type. I could do it as object, but then I lose the type checking. I could expose it as a SqlType, but that is going to cause me casting problems and also introduce errors that the compiler can't check for.

I put a property beside the propery to allow a programmer to set the property to null, but this sucks as I have to have two properties along side each other for really what should be one property. (This is what is done in a strong typed dataset.)

After doing the above, I try to use my Strongly Typed collection (inherits from CollectionBase and implements the IBindable interface) as a datasource for datagrids. It blows up of course because I throw an error when the datagrid accesses the int property that is null.

What do I do?
# Sven's Blog said on April 8, 2005 12:29 AM:
After reading two blog posts ([1], [2])&amp;nbsp;about Nullable Types and consulting the beta documentation...
# Frans Bouma's blog said on April 16, 2005 4:59 AM:
# Frans Bouma's blog said on April 16, 2005 5:28 AM:
# Dennis' blog said on May 4, 2005 4:08 AM:
# The Visual Basic Team : Nullable types in VB as Well at Restaurants said on December 31, 2007 2:50 AM:

PingBack from http://restaurants.247blogging.info/?p=648

# Nullable Types | keyongtech said on January 22, 2009 4:15 AM:

PingBack from http://www.keyongtech.com/656315-nullable-types

# The Visual Basic Team Nullable types in VB as Well | Patio Chairs said on June 2, 2009 10:43 PM:

PingBack from http://patiochairsite.info/story.php?id=29265

# The Visual Basic Team Nullable types in VB as Well | adirondack chairs said on June 14, 2009 9:11 AM:

PingBack from http://adirondackchairshub.info/story.php?id=813

# The Visual Basic Team Nullable types in VB as Well | patio set said on June 17, 2009 10:01 PM:

PingBack from http://patiosetsite.info/story.php?id=90

# The Visual Basic Team Nullable types in VB as Well | patio umbrella said on June 18, 2009 12:03 AM:

PingBack from http://patioumbrellasource.info/story.php?id=1768

# The Visual Basic Team Nullable types in VB as Well | garden decor said on June 19, 2009 3:19 AM:

PingBack from http://gardendecordesign.info/story.php?id=5164

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker