An Orcas Preview: Go Inside the Next Version of VB

Microsoft is promising some exciting new features for Visual Basic 9 (codename: Orcas). The enhancements will include better-integrated support for handling data, increased support for dynamic typing, and language features that will reduce code redundancy and improve programmer productivity. In fact, the release offers too many great new features for any one article to cover, so this preview focuses on a few of my favorites:

  • Object initializers
  • Implicitly typed local variables
  • LINQ
  • Query comprehensions
  • Nullable types

Object Initializers

Object intializers are conceptually similar to Visual Basic's With keyword. They facilitate the creation of complex object instances by combining the definition and the initialization of the object instance into one statement.

For demonstration, take the following basic Employee class, which contains three properties:

Class Employee
   Public Property Name As String
   Public Property ID As Long
   Public Property Department As String
End Class

To create and initialize an Employee object, you can do the following:

Dim e = New Employee {
                .Name = "Joe Smith", _
                .ID = 123456789, _
                .Department = "Accounting", _
         }

You also can use nested object initializers to instantiate multiple objects at one time. For example, to create a list of employees, you could do the following:

Dim Emps = New List(Of Employee){
                   { .Name = "Mary Jones", _
                     .ID = 123456789, _
                     .Department = "Accounting"}, _
                   { .Name = "Joe Smith", _
                     .ID = 123456788, _
                     .Department = "Accounting"}, _
                   { .Name = "Mike Brady", _
                     .ID = 123456787, _
                     .Department = "IT"}, _
                   { .Name = "Kim Thomas", _
                     .ID = 123456786, _
                     .Department = "IT"}
           }

Implicitly Typed Local Variables

The inclusion of implicitly typed local variables makes code more readable. Through implicitly typed variables, the compiler determines the variable type based on the initializer expression or value. Implicitly typed local variables work by default, regardless of the Option Strict setting. In fact, in VB 9 you must explicitly specify late binding by declaring a variable as type Object.

As an example, you can print the names of all the employees from the class created and instantiated in the previous section. Prior to VB 9, you would do something like this:

For Each e As Employee In Emps
   Console.WriteLine(e.Name)
Next

In VB 9, you can write it like this:

For Each Dim e In Emps
   Console.WriteLine(e.Name)
Next

I'll grant that the difference is subtle, but I believe the VB 9 version is more readable.

LINQ

A large percentage of applications interact with and manipulate data that is stored in some type of relational database or XML file. A major frustration for VB programmers, and programmers of most other languages as well, is that to get to the data they have to utilize APIs—for example, ODBC—and then write SQL queries as strings within their applications. Using this method works, but you miss out on some things, such as compile-time verification of your queries and productivity enhancements like IntelliSense.

Some larger issues are in play as well. The .NET versions of Visual Basic place a heavy emphasis on object-oriented programming, defining and manipulating data through the use of objects. Relational databases use rows of data that exist in tables. Bridging the gap between objects and relational data often has been a source of frustration for the programmer.

Microsoft's answer to this conundrum is LINQ (Language INtegrated Query). LINQ provides the infrastructure within the .NET Framework for managing various sources of data as objects. It accomplishes this by translating language-integrated queries into SQL queries that are executed against the database. It then translates the results of the query into objects.

The great thing about LINQ is that it will provide a common query language for use against relational databases, XML, and objects. Instead of just adding relational or XML-specific features, Microsoft took a step back to look at the bigger picture. By recognizing the commonality among relational, XML, and object-based data, they were able to take a more general approach and add general-purpose query features to the .NET Framework.

Query Comprehensions

Query comprehensions provide an SQL-like syntax for queries. Those familiar with SQL will instantly recognize many of the operators. The key difference is that these are an integrated part of the language, allowing for the use of features like IntelliSense.

Now, suppose you need to get a list of all the employees from the accounting department. Using the Employee class defined previously, you would write something like this:

Dim Accounting = From e In Emps _
                 Where e.Deparment = "Accounting" _
                 Order By e.Name _
                 Select e

For Each Dim e In Accounting
   Console.WriteLine(e.Department & " : " & e.Name)
Next

The results would be as follows:

Accounting : Joe Smith
Accounting : Mary Jones

You probably noticed the query syntax is slightly different from what you would see in pure SQL. The Select statement comes at the end instead of the beginning as it does in SQL. Moving Select to the end allows better support for IntelliSense. Paul Vick, the Technical Lead on the Visual Basic .NET product team at Microsoft, has a great blog called Panopticon Central where he has written a couple posts concerning this.

The integrated query language also supports aggregate operators such as Min, Max, Count, Sum, Avg, and so forth. To get a count of the number of employees, you would write a query like this:

Dim countOfEmps As Long = From e In Emps _
                          Select Count(e)

Microsoft did a great thing by making the integrated query language so similar to SQL. Any application developer who works with databases knows at least a little SQL, and the similarities to SQL should make the transition to LINQ fairly smooth.

An Orcas Preview: Go Inside the Next Version of VB

Nullable Types

Relational databases allow null values for all kinds of data types. Most programming languages have no built-in facility for handling null values. For example, a variable of type Integer in VB must always have some value; it can never be Nothing. To address this problem, the CLR has added support for nullable types. To declare a variable as nullable in VB 9, you must put a ? at the end of either the variable name or the type name.

Now, modify the Employee class to contain a StartDate and an EndDate. StartDate is obviously the date an employee started working, and so it will never be Nothing. However, EndDate will not have a value until an employee no longer works for the company, so it will be Nothing until then. The new version of the Employee class looks like this:

Class Employee
   Public Property Name As String
   Public Property ID As Long
   Public Property Department As String
   Public Property StartDate As Date
   Public Property EndDate As Date?
End Class

You also could write the EndDate declaration as follows:

Public Property EndDate? As Date

To create and initialize a new Employee object, you can do this:

Dim e = New Employee {
            .Name = "Joe Smith", _
            .ID = 123456789, _
            .Department = "Accounting", _
            .StartDate = #1/1/1995#, _
            .EndDate = Nothing
        }

As you can see, this is very useful because you no longer have to use some arbitrary date in the distant past or far future (in other words, 1/1/1900 or 12/31/3000) as a null value. It also allows objects to more closely represent the data they are based on and frees the programmer from having to worry about translating null values between the database and the application.

Find Out More

Visual Basic 9 will offer many new exciting features, especially in the areas of data access and management. I believe the planned improvements will make a great language even better. I'm excited to see Microsoft putting so much effort into VB and trying to make developers' lives easier. Although this preview has touched on only a few of my personal favorite new features, I hope it has enticed you to further explore what's coming in VB 9.

What's in a Name?

For those of you curious about the Orcas codename, Microsoft names early versions of products after geographical features in the northwestern United States. Orcas, and its older sibling VB 8 (codename: Whidbey), are both islands located off the coast of Washington.

About the Author

Josh Fitzgerald is an applications development group leader for a large medical device company in Warsaw, Indiana. Designing and developing Visual Basic .NET applications is only one of his responsibilities, but it is his favorite part of his job. You can reach Josh at josh.fitzgerald@gmail.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds