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.