Three Fantastic Updates to VB

Microsoft has made it very clear that as time goes on, C# and VB will get new language features at more or less the same time. In fact, with the release of .NET 4.0, VB and C# are extreamly close in terms of overall functionaltiy (however, VB is still the sole owner of XML literals- sorry C# programmers).

VB 2010 has three excellent updates.  The first update is very minor in scope, but leaves a *huge* impact.  Simply put, the dreaded "line continuation character" is no longer needed!  Yet it is true, VB programmers can now define a single code statement which spans over multiple lines without littering your code files with _ tokens.

The other two updates have to do with how VB handles lambda expressions. As you might recall, under .NET 3.5, VB's lambda support was extreamly limited.  Specifically, you would only use a lambda the arguments were processed by a single code statement, and furthermore, this single statement must supply a return value. Under .NET 4.0, VB lambdas are just as expressive a C#'s. Specifically, you can now create multi-statement lambdas, as well as lambdas which represet Subs.

To illustrate all three of these features working together, consider the following VB code snippet which is downloading an e-book using the WebClient class (in the System.Net namespace) in an async manner.

Code Snippet
  1. Private Sub btnDownload_Click(ByVal sender As System.Object,
  2.       ByVal e As System.EventArgs) Handles btnDownload.Click
  3.  
  4.         ' Create a WebClient object.
  5.         Dim wc As New WebClient()
  6.  
  7.         ' Handle the following event, which will
  8.         ' store the text of the e-book once downloaded.
  9.   AddHandler wc.DownloadStringCompleted, Sub(s, eArgs)
  10.         theEBook = eArgs.Result
  11.         txtBook.Text = theEBook
  12.     End Sub
  13.  
  14.     ' The Project Gutenberg EBook of A Tale of Two Cities, by Charles Dickens.
  15.     ' Download this book in the background on a secondary thread.
  16.   wc.DownloadStringAsync(New
  17.         Uri("http://www.gutenberg.org/files/98/98-8.txt"))
  18. End Sub

Here, we are handling the DownloadStringCompleted event using the AddHandler statement. However, notice that we have a multi-line lambda, with no return value!  Even better, notice how we have no trace of the dreaded underbar.


Posted by: Andrew Troelsen
Posted on: 4/8/2010 at 2:10 PM
Tags:
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Subscribe to this BlogRSS comment feed

Add comment




biuquote
  • Comment
  • Preview
Loading


Contact Us 651-994-8558 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Prism / MVVM / MEF Training | Microsoft Web Development Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.