Introduction
Earlier this month Microsoft released Visual Studio 2010, the .NET Framework 4.0 (which includes ASP.NET 4.0), and new versions of their core programming languages: C# 4.0 and Visual Basic 10 (also referred to as Visual Basic 2010). Previously, the C# and Visual Basic programming languages were managed by two separate teams within Microsoft, which helps explain why features found in one language was not necessarily found in the other. For example, C# 3.0 introduced collection initializers, which enable developers to define the contents of a collection when declaring it; however, Visual Basic 9 did not support collection initializers. Conversely, Visual Basic has long supported optional parameters in methods, whereas C# did not.
Recently, Microsoft merged the Visual Basic and C# teams to help ensure that C# and Visual Basic grow together. As explained by Microsoft program manager Jonathan Aneja, "The intent is to make the languages advance together. When major functionality is introduced in one language, it should appear in the other as well. ... [T]hat any task you can do in one language should be as simple in the other." To this end, with version 4.0 C# now supports optional parameters and named arguments, two features that have long been part of Visual Basic's vernacular. And, likewise, Visual Basic has been updated to include a number of C# features that it was previously missing.
This article explores some of these new features that were added to Visual Basic 2010. Read on to learn more!
Implicit Line Continuation (It's About Time!)
All programming languages use some character to denote the end of a statement. C-flavored languages (like C#) use the semicolon to mark the end of a statement, whereas the Basic programming language has long used the carriage return. One downside of using the carriage return as the statement delimiter is that it prohibits developers from having a single statement span multiple lines.
Consider the following function declaration:
Public Function ChangePassword(ByVal Username As String, ByVal OldPassword As String, ByVal NewPassword As String) As Boolean |
We could improve the readability of the function declaration by having each input parameter on its own line. But doing so would confuse the compiler, as it presumes that a carriage return denotes the end of a statement. In the past, Visual Basic allowed for a single statement to be spread over multiple lines by using the line continuation character, an underscore (_
). Using the line continuation character we could rewrite the above function declaration to span multiple lines like so:
Public Function ChangePassword(ByVal Username As String, _ |
Note the underscore after the Username
and OldPassword
input parameters.
Visual Basic 2010 still supports the use of the underscore as an explicit line continuation character, but it also supports implicit line continuation. Thanks to implicit line continuation in VB 2010, you can now continue lines of code on a new line without having to use the underscore character. For example, the above function declaration could be rewritten to omit the underscores, as the below screen shot illustrates.
In short, the Visual Basic 2010 compiler allows you to omit the underscore when continuing a statement on the next consecutive line. It's natural to wonder how VB can distinguish between a carriage return that signifies a line continuation and one that signifies the end of a statement. There are certain syntax elements that cannot be used to end a statement and if VB notes a carriage return after one of these it can safely assume that the carriage return is meant as a line continuation character and not as the end of the statement. The following table lists the some of the syntax elements that, when followed by a carriage return, is treated as an implicit line continuation character by the compiler.
After a comma | After an open parenthesis or before a closing parenthesis | After an open curly brace or before a closing curly brace |
After an open embedded expression ( <%= ) or before the close of an embedded expression (%> ) within an XML literal | After the concatenation operator ( & ) | After assignment operators ( = , &= , += , ...) |
After binary operators ( + , - , And , Or , ...) within an expression | After the Is and IsNot operators | After a member qualifier character ( . ) and before the member name |
For a complete list of the syntax elements as well as for examples of the above syntax elements in use, refer to the Statements in Visual Basic documentation's "Implicit Line Continuation" section.
Auto-Implemented Properties
When creating a class it's quite common to have one or more properties that don't include any logic in their getters or setters; instead, the property serves merely as a mechanism to expose a member variable. Most every Visual Basic developer has written code that looks like the following at some point or another:
Public Class Product |
Having to write all that boilerplate property code seems redundant and a waste of time. What made it more frustrating was that C# 3.0 introduced auto-implemented properties, which allowed C# developers to create such simple properties with far less syntax. For more information on C#'s auto-implemented properties, consult Auto-Implemented Properties (C# Programming Guide).
The good news is that VB 2010 now has auto-implemented properties! You can turn the above property and private member variable declarations into just two lines of code using this new feature. To use auto-implemented properties use the following syntax:
Property PropertyName As Type |
That's all there is to it! The screen shot below shows the much more concise property syntax for the Product
class created above. Note how auto-implemented properties allowed us to reduce the property-related lines of code from 18 down to two!
Auto-implemented properties are an example of syntactic sugar - the new syntax makes the code easier to read and write for us developers, but, behind the scenes, it's business as usual. Specifically, the VB compiler automatically converts the auto-implemented property syntax into a private member variable and an expanded property with getters and setters.
One thing to note is that there are some subtle differences between VB's auto-implemented properties and C#'s. C# allows for read-only and write-only auto-implemented properties; it also allows different access methods (public
, private
, etc.) for the getters and setters. Visual Basic does not provide such flexibility. If you need to define read-only or write-only properties, or need different accessibility levels for the getter and setter, you'll need to use the expanded property definition syntax. However, with Visual Basic's auto-implemented properties you can specify an initial value for the property like so:
Property PropertyName As Type = Initial_Value |
The following screen shot shows the syntax to specify an initial value for the ProductName
and UnitCost
properties in the Person
class, namely "Chai" and 4.95, respectively.
Collection Initializers
Both C# 3.0 and Visual Basic 9 introduced object initializers, which allow developers to declare and assign default values to the new object's properties all in one statement. C# 3.0 also added collection initializers, which allow developers to declare a collection and add elements to it in one statement; unfortunately, VB 9 did not include support for collection initializers. The good news is that collection initializers have been added to Visual Basic 2010!
Prior to VB 2010, creating a collection and specifying its elements required quite a few lines of code. The following code snippet shows the declaration of a List
of Integers
named Fib
that is populated with the first eight Fibonacci numbers:
Dim Fib As New List(Of Integer) |
Using collection initializers, the above syntax can be greatly compressed. Use the From
keyword to specify the collection's initial elements. The following screen shot shows VB 2010 code that uses collection initializers to reduce the sheer volume of code and to improve the code's readability.
And, if you like, you can also use implicit line continuation to separate out the initial values on separate lines:
Conclusion
With it's release of Visual Studio 2010 and the .NET Framework 4, Microsoft has also released the latest versions of its two most popular .NET programming languages: C# and Visual Basic. Over the past couple of years Microsoft has worked to help unify the C# and Visual Basic feature sets and to ensure that these two languages grow together. To this end, Visual Basic has been updated with a number of language enhancements that were previously added to C#, including implicit line continuation, auto-implemented properties, and collection initializers. Conversely, C# has been updated to include a number of VB features that were previously missing. A future article will explore these C# 4.0 additions
No comments:
Post a Comment
Suggestions are invited from readers