Nullable types in VB as Well

Nullable types in VB as Well

  • Comments 20
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
Leave a Comment
  • Please add 2 and 8 and type the answer here:
  • Post
  • good work. let's see more technical posts on the vb team blog.
  • Great!
  • 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)
  • Nothing doing.
  • 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>
  • 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



  • 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?
  • After reading two blog posts ([1], [2])&amp;nbsp;about Nullable Types and consulting the beta documentation...
  • PingBack from http://restaurants.247blogging.info/?p=648

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

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

Page 1 of 2 (20 items) 12