dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

What's New in Visual Basic 9.0? Part 1

The release of Visual Studio 2008 updates Visual Basic to version 9.0, with several key language enhancements and LINQ support. Part one of this series will walk you through using nullable types, type inference, the If operator, and other time-saving enhancements.


advertisement
ith the release of Visual Studio 2008, Microsoft has also updated the VB language to its latest version, 9.0. In VB 9.0, there are several key language enhancements that have been made to support the new Language Integrated Query (LINQ) feature announced earlier by Microsoft. This article will walk you through each of these new language enhancements and provide a couple of code examples to illustrate their uses.

Nullable Type
As you are no doubt aware, all un-initialized value types in VB have a default value when they are declared. For example, the following declaration declares a Boolean variable:


        Dim married As Boolean
Because it has not been initialized, married now contains the default value of False. However, there are times where you do not know the marital status of a person and hence the variable should neither be True nor False. In VB 9.0, you can now declare value types to be nullable, i.e., they do not yet have a value.

To make the married variable nullable, the above declaration can be rewritten in three different ways (all are equivalent):


        Dim married As Boolean?
        Dim married? As Boolean
        Dim married As Nullable(Of Boolean)
In this case, married can take one of the three values: True, False, or Nothing. The following code snippet will print out "Not Married":

        If married Then
            MsgBox("Married")
        Else
            MsgBox("Not Married") '---this will be printed---
        End If
This is because the If statement evaluates to a False (married is currently Nothing) and hence the Else block will execute. A much better way to check would be to use the following snippet:

        If married Then
            MsgBox("Married")
        ElseIf Not married Then
            MsgBox("Not Married")
        Else
            MsgBox("Not sure") '---this will be printed---
        End If
Once a nullable type variable is set to a value, you can set it back to nothing by using Nothing, as the following example shows:

        married = True    '---set it to True---
        married = Nothing '---reset it back to nothing---
To check the value of a nullable variable, use the HasValue property, like this:

        If married.HasValue Then
            '---this line will be executed only 
            'if married is either True or False---
            MsgBox(married.Value)
        End If
Do not use the "=" operator to test against Nothing, as this will always evaluate to Nothing, like the following example shows:

        If married = Nothing Then
            '---this line will NEVER be executed---
            MsgBox(married.Value)
        End If
Instead, you should use either the Is or IsNot operator:

        If married IsNot Nothing Then
            '---this line will be executed only 
            'if married is either True or False---
            MsgBox(married.Value)
        End If



Thanks for your registration, follow us on our social networks to keep up-to-date