The release of Microsoft Visual Basic 9.0 introduces
enhancements to the language to better support Language Integrated Query
(LINQ). As with C# 3.0, the changes to Visual Basic 9.0 do not require
modification of the common language runtime (CLR).
In this article, we will examine the new syntax available in Visual
Basic 9.0, comparing it to the C# equivalent whenever possible. As you will
see, there are some differences between these languages, and some features are
not present in both. Even if you are a C# programmer, please take a look at
this article. You will discover that sometimes you can take advantage of Visual
Basic 9.0 rather than C#, or at least you will be able to read Visual Basic
code for LINQ.
If you prefer to use Visual Basic, remember that knowledge of the
whole set of Visual Basic 8.0 features is necessary to effectively use the new
Visual Basic 9.0 syntax. The ability to read C# code is also important for
reading the rest of this book (because LINQ examples are written in C#) and for
understanding differences between C# 3.0 and Visual Basic 9.0.
Visual Basic 9.0 and Nullable Types
Visual Basic 9.0 includes features that have been available
in C# since version 2.0. One of these features-nullable types-is often useful
with LINQ; therefore, we want to quickly describe nullable types here.
Since the introduction of generics in Microsoft .NET 2.0, the CLR
offered the generic class Nullable(Of T As Struct ) to
programmers who wanted to add the semantic of NULL to a value type. Declaring a
variable of this type, you can assign the NULL “value” without having to define
a new type for this sole purpose. In this way, handling NULLs in data becomes
similar to how it is done in SQL.
C# 2.0 added direct support in the language to enable the use of
this type: if you simply add the ? suffix to the name
of the type, your compiled code will use the Nullable generic
class instantiated for the requested type. Visual Basic 9.0 offers the same
capabilities with a similar syntax, as you can see in
Listing 3-1.
Listing 3-1: Nullable
type declarations
Dim a As Integer? = 18
Dim b As Integer? = 24
Dim c As Integer? = Nothing
Dim d As Integer?
d = a + c ' d = 18 + Nothing = Nothing
c = a + b ' c = 18 + 24 = 42
A regular Integer variable cannot be
assigned to Nothing, because Integer
is a value type. In Listing 3-1, we
assigned c to Nothing, and we
used it in a calculation to assign d. The result of
a + c is Nothing, showing the three-valued
logic that is typical of the nullable types. The NULL “value” (represented by
Nothing in Visual Basic) is propagated into an expression, with some
exceptions using logical operators.
A nullable value cannot be assigned to the corresponding
non-nullable type. The code in Listing 3-2
shows the required conversions.
Listing 3-2: Nullable
type conversions
Dim k As Integer? = 16
Dim p As Integer = k ' Compiler error
Dim q As Integer = DirectCast( k, Integer ) ' Ok
Dim r As Integer = CType( k, Integer ) ' Ok
Please refer to the Visual Basic documentation for more
information about the nullable types in Visual Basic 9.0. We expect this
feature to be used extensively in code that manipulates data back and forth
from a relational database.
|