Thursday, August 30, 2007 12:00 AM bart

Visual Basic 9.0 Feature Focus - Nullable types

Welcome back to the Visual Basic 9.0 Feature Focus blog series. In this post, we'll cover Nullable Types, a feature that has been available in C# since version 2.0. Nullable types help to close the gap between value types and typical database representations of these, which allow values to types not to be set. There are two big categories of types in the CLS, and thus in Visual Basic too: reference types and value types. A simple of the former one is String while numerical types like Integer are samples of value types. Reference types can point to nowhere using "Nothing", while value types should have a value from their value domain (e.g. -2^32 to 2^32 - 1). So what do you do with a Customer object that has an Age property declared as an Integer but it's possible to have a missing age? Various tricks are possible: keep an additional Boolean value to indicate whether or not the value has been set, reserve a special value to indicate a value isn't set (e.g. Age = -1), ...

However, nullable types are much easier and basically wrap value types in a generic object of type Nullable(Of T) which is a structure that can have Nothing as its "value" (with aid from the compiler in case of C# - see section 6.1.5 in the spec). In fact, you could use the Nullable(Of T) class already in VB 8.0:

Dim i As Nullable(Of Integer)
Dim b As Boolean

i = Nothing
b = i.HasValue 'Will be False

i = 123
b = i.HasValue 'Will be True
Dim j As Integer = i.Value 'j = 123

However, where VB 8.0 is different from C# 2.0 is the lack of integrated syntax to denote a nullable type: you have to write Nullable(Of ...) manually each time. In VB 9.0 this has been solved:

Dim i As Integer?

The question mark is used to make a Nullable for the specified value type:

image

Notice that if a value has been assigned to a nullable variable, you can just write the following to get the value back:

Dim j As Integer = i 'j = 123

There's no need to call the Value property explicitly. If you try to get the value of an unset nullable variable, you'll get an exception thrown in your face:

image

The use of nullables goes even further by means of three-valued logic and null propagation when working with arithmetic operations on nullable value types. Take a look at the following fragment:

Dim a As Integer? = Nothing
Dim b As Integer? = 123
Dim c As Integer? = a + b

In here c will get the 'value' Nothing. Thus, whenever you apply arithmetic operations on one or more nullables of which one is null-valued (Nothing), the result will be the null value too.

Nullable types are especially useful when mapping database schemas to objects, as is done in LINQ to SQL for example. Tomorrow, we'll look at the Ternary Operator 'If' which makes working with these Nullable Types easier in some cases.

Happy coding!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Comments

# re: Visual Basic 9.0 Feature Focus - Nullable types

Thursday, August 30, 2007 2:03 AM by Sergei Almazov

"a generic object of type Nullable(Of T) which is a reference type"

Nullable(Of T) is a value-type, NOT reference type. From Reflector:

Public Structure Nullable(Of T As Structure)

...

# 9 Links Today (2007-08-30)

Thursday, August 30, 2007 8:20 AM by 9 Links Today (2007-08-30)

Pingback from  9 Links Today (2007-08-30)

# University Update-David Stremme-Visual Basic 9.0 Feature Focus - Nullable types

Pingback from  University Update-David Stremme-Visual Basic 9.0 Feature Focus - Nullable types

# re: Visual Basic 9.0 Feature Focus - Nullable types

Thursday, August 30, 2007 10:07 AM by bart

Hi Sergei,

You're right about this; from a runtime's perspective it's a struct, but from a language perspective you can use it as if it's a reference type (that is, you don't get a CS0037 error when assigning null in C# -- VB takes a slightly different approach for Nothing assignments to structures, which is why the VB 8.0 code fragment works). I've changed the post accordingly.

-Bart

# New Features of Visual Basic 9 Article Series

Thursday, September 27, 2007 5:47 AM by Walter Stiers - Academic Relations Team (BeLux)

In a series of 15 posts, Bart De Smet explores several of the new features in Visual Basic 9 . These

# Visual Basic 9.0 Feature Focus - The If Ternary Operator

Thursday, October 11, 2007 3:31 PM by Elan Hasson's Favorite Blogs

Welcome back to the Visual Basic 9.0 Feature Focus blog series. In this post, we'll cover the If

# Recap of European Launch Events for Visual Studio 2008 (Amanda Silver)

Monday, March 24, 2008 4:44 PM by The Visual Basic Team

Last week I had the privilege of speaking in Lisbon, Portugal & Birmingham, England at launch events

# MSDN Blog Postings » Recap of European Launch Events for Visual Studio 2008 (Amanda Silver)

Pingback from  MSDN Blog Postings  » Recap of European Launch Events for Visual Studio 2008 (Amanda Silver)

# ヨーロッパでの Visual Studio 2008 発表イベントの要約 (Amanda Silver)

Monday, June 09, 2008 11:42 PM by The Visual Basic Team

先週、 ポルトガルのリスボン と イギリスのバーミンガム での Visual Studio 2008 の発表イベントで講演する機会がありました。どちらも熱狂的な雰囲気に包まれていたのは、今回の Visual

# VB 9.0 Feature Focus – Link Collection

Saturday, August 09, 2008 7:16 AM by B# .NET Blog

Collecting a few of my posts for easy quick reference: Visual Basic 9.0 Feature Focus – Introduction