Home » VB.NET » VB.NET Nullable Types

VB.NET Nullable Types

tp_vol4_001

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

Related Post

  • VB.NET – Char from String with Option StrictVB.NET – Char from String with Option Strict So here's the question: I'm using String.Split() and need to pass in a Char or a Char array as the parameter.  If I pass in a string String.Split("/") I get an error "Option Strict […]
  • Value Type vs Reference TypeValue Type vs Reference Type It is amazing to me how few programmers understand the fundamentals of how variables work.  Not just in .NET or C# specifically, but in every language they work in.  It amazes me for two […]
  • VB.NET Processing Before WinForm DisplayVB.NET Processing Before WinForm Display I woke up this morning to an interesting question. “Using VB.net 2008, I want my project to be a Windows Forms Application, but upon startup, I want to check a few files to see if they […]
  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • Manually Adding Event Handlers in VB.NETManually Adding Event Handlers in VB.NET Typically when we write our code, the event handlers get wired up for us using the handles clause.  So we never have to worry about wiring up our event handlers manually. But what […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.