advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
Rate this item | 0 users have rated this item.
Email this articleEmail this article
 
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

  Next Page: Type Inference
Page 1: IntroductionPage 4: Extension Methods
Page 2: Type InferencePage 5: Lambda Expressions
Page 3: Object Initializers 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES