Nullable Types in VS2008

Filed under .NET, VB Feng Shui

The VS 2008 VB will support nullable types.

Simply put, a nullable type is a standard type, like, say, integer or string that can ALSO contain a null.

This can become very important in database work, because a null value in a field means that no value has been assigned to that field vs a common case where you might use 0 in a numeric field to indicate no value, or you might use a null string (ie a string with no length) in a string field.

Obviously, using 0 to indicate no value works much of the time, but, there often comes a point when you actually do need to use a 0 value for that field.  There’s a number of other, more arcane reasons that using one of the values from the range of possible values of a type to indicate null is usually not a good idea.

Hence, nullable types. With a nullable type, I can have an integer variable that can be a 31bit positive or negative number, or 0, and STILL have yet another “state” of that variable that indicates it’s null.

This will likely be incredibly handy for many many uses when working with database data.

But the syntax?

Dim a As Integer?

or

Dim a? As Integer

No, I’m not making that up.

What the hell? A question mark?

Why not just:

Dim a As Nullable Integer

I find this to be an unbelievably hypocritical decision given the almost overwhelming philosophical rants delivered by Microsoft about not using the traditional VB type specifier characters in .NET anymore (you know, $ for strings, % for integers, those bad boys). Heck, check page 42 of “Framework Design Guidelines” by Krzysztof Cwalina and Brad Abrams of Microsoft:

DO NOT use Hungarian notation (their bold, not mine)

Now, to be fair, the authors in this particular instance are discussing publicly available interfaces, but I’ve seen the same directive applied to internal coding as well. So, we’re not suppose to use hungarian, and we’re not supposed to use type characters, so we’re left to simply scan the code to information about how a variable is used or what it likely contains. Jeez. How convenient.

4 Comments

  1. Ralf says:

    Little known fact: Dick Cheney runs MSFT behind the scenes. Yup! It’s just another arm of the NSA and answers directly to the office of the Vice President. Cheney was a supporter the Hungarian regime as late as 1999, until mysteriously declaring them "unacceptable".

    Also explains why all the EULAs now contain the text "Please speak slowly and clearly into your computer’s microphone."

  2. Ralf says:

    Oh, and nullable variables are an excellent solution to an ongoing headache. I am soooo tired to Null-checking all my database apps. Even today I get surprised by bugs that make it into production that would have been avoided by using a nullable variable.

    I just hope they’re faster than Variants.

  3. Darin says:

    I’d hope they’re faster than variants, but I haven’t read of any performance tests on nullable types yet.

    Ages ago, I wrote a wrapper class for the RecordSet object that dealt with all those things like transactions, parameterized queries, and especially null checking field values. I made the default field getter cast nulls to "" or 0, depending on the field type.

    Separately, you could call a method to detect nulls, for those few instances where you actually needed to.

    To me, nulls have always been more important to the querying aspects of DB work than the actual user presentation of data.

    Maybe I’ve just been lucky?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*