SQL has long had the ability to specify that a value is NULL even if it is a primitive type, but the only way you could have a NULL value in VB.NET is if you were dealing with an object.
That is, until .NET 2.0
Even though .NET 2.0 has been out for a while, I would bet that few VB.NET programmers know about this new feature because it is one of those things most of us have grown to assume is not possible.
Values must have content–objects don’t. That’s just the way it is.
If we were to create an integer for example and then tried to assign a nothing to it, we would most certainly get a compiler error.
Dim i As Integer i = Nothing
However, the Nullable generic was added so that we can create a VB.NET Nullable Integer
Dim i As Nullable(Of Integer) i = Nothing
And by simply putting a question mark at the end of our variable name, we can shorten the syntax to
Dim i? As Integer i = Nothing
Other post in VB.NET
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- The VB.NET Ternary Operator - November 29th, 2007
- UnauthorizedAccessException writing to HKLM - April 8th, 2008
- Overriding Events in VB.NET - May 8th, 2008
- VB.NET - Char from String with Option Strict - April 8th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- Manually Adding Event Handlers in VB.NET - July 15th, 2009
- VB.NET Processing Before WinForm Display - August 6th, 2009
- Decimal vs Float (Single) or Double - September 18th, 2012
- VB.NET Hide Module Name - August 28th, 2013
- VB.NET Nullable Types - January 15th, 2014