Code Focused

Hidden Improvements in Visual Basic 11

Visual Basic 11 contains many enhancements you may not have noticed.

In my January article "Visual Basic Improvements in Visual Studio 11," I presented the major enhancements to Visual Basic 11 that appear in the Visual Studio 11 Beta. Key improvements include the Async and Await keywords, Iterators, Call Hierarchy and the Global keyword.

In February, I attended the 2012 Microsoft Global MVP Summit in Redmond. At the event, Microsoft Visual Basic Program Manager Lucian Wischik presented a four page list of enhancements in Visual Basic 11. Here are a few of the best VB11 updates from his list, enhancements that you may not have noticed.

MSTest and XUnit now support Async and Await in unit tests. Prior to this support, the test method did not wait for the Await method to complete, potentially executing the Assert method too soon. Now, the Async method will complete and the Assert behaves as expected.

Security is enhanced to ensure that any framework method that delay-executes a delegate, does so in the proper SecurityContext. The number of heap allocations is reduced, which benefits server scalability.

Compiler Switches
The compiler switch "/vbruntime:*" embeds core functionality from the Visual Basic Runtime Library into your assembly to provide useful Visual Basic functions without requiring a reference to the runtime library. This new functionality opens Visual Basic to target platforms that do not support the Visual Basic runtime like the Portable Library, XNA, Windows Phone and the Micro Framework. With this switch, any use of Visual Basic features outside the embedded core functionality will result in a compiler error.

The compiler switch "/platform: anycpu32bitpreferred" was added as a better alternative to the AnyCPU setting. Many developers change the default Target CPU from x86 to AnyCPU, thinking that will provide optimum performance in both x86 and x64 operating system (OS) environments. However, the performance penalty for running 64-bit code is rarely worth it. It's justified only when large memory space is truly needed. The vast majority of applications will run faster as 32-bit applications, even in a 64-bit OS. This new compiler switch favors the benefits of x86 on an Intel CPU, but still permits deployment to other CPUs such as ARM. Note that due to a bug in VB11, this option is not available in Visual Studio's Project Properties Compile tab Target CPU field, it can only be set with the vbc.exe command line compiler.

The compiler switch "/highentropyva" marks the assembly header with a flag instructing the OS to perform additional randomization of the memory layout of the executable, making it less vulnerable to some security attacks. This switch is on by default.

Optional Parameter Overloads
The following overloads are now legal in VB11. In VB10, they generate the error message shown in Figure 1.
  Private Sub f(x As Integer)  
           End Sub  Private Sub f(x As Integer, 
           Optional y As Integer = 0)  End Sub
Figure 1. Visual Studio 2010 overloads error removed in Visual Studio 11 Beta
Read-Only Types
Three read-only types were added to the .NET Framework for compatibility with WinRT applications: IReadOnlyCollecton(Of T), IReadOnlyList(Of T) and IReadOnlyDictionary(Of K,V). Sample code to initialize the three structures is as follows:

  Private Sub NewVB11Types()  
   'Initialize the ReadOnlyCollection from a mutable collection    
   Dim VSMAuthors As New List(Of String)(New String() {       
   "Brust", "Kunk", "Nasr", "Papa", "Steele", "Vogel"})    
   Dim VSMColumns As New Dictionary(Of String, String) From {        
   {"C# Corner", ""}, {"On VB", ""}}    
   Dim roc As New System.Collections.ObjectModel.ReadOnlyCollection(Of String)(VSMAuthors)    
   Dim rol As System.Collections.Generic.IReadOnlyList(Of String) = VSMAuthors    
Dim rod As System.Collections.Generic.IReadOnlyDictionary(Of String, String) = VSMColumns
End Sub
Type Inference for Return Array Literals
Visual Basic 11 extends type inference to array literals as shown in the following code. In Visual Studio 2010, the "Return {}" statement would have been typed as an Object array incompatible with the required return type of Integer array and generated the error shown in Figure 2. Under Visual Studio 11, this sample code builds fine.

  Function f(i As Integer) As Integer()      
   If i = 0 Then Return {}      
   Return {1, 2, 3}  End Function
Figure 2. Array type inference error in Visual Studio 2010
Bug Fixes
Nearly 200 bugs were fixed in Visual Studio 11, including many situations that caused Visual Studio to crash: error reporting, closing a project, referencing an obfuscated assembly, using NoPIA (type embedding) and huge string concatenation. Visual Studio 2010 was the first release of the WPF-based user interface, so these crash bugs were not unexpected, and they were quickly addressed.

How can you best report errors and enhancement suggestions to Microsoft to help improve future versions of Visual Studio and the .NET languages? If there is a crash, allow the Watson report of the crash to be sent to Microsoft. These reports are aggregated, and the ones that occur most frequently get automatically created as bugs.

Bugs are sometimes drawn from StackOverflow or from blogs, but this is not a reliable way to get Microsoft's attention. It's better to file your bug or enhancement idea on Microsoft Connect or the Visual Basic Language forum. These sites get the greatest attention from Microsoft.

Explore the Visual Basic 11 Beta
I've listed just some of the lesser known improvements in the Visual Basic 11 Beta. If you have not done so already, download your copy of the Windows 8 Consumer Preview and the Visual Studio 11 Beta. I'm sure you'll enjoy these enhancements and find even more language goodies than I've discussed here.

comments powered by Disqus

Reader Comments:

Wed, Jan 30, 2013 Ival cheap auto insurance 1680 car insurance mbkxa viagra xxej DOT 1680 DOT mbkxa DOT xxej

Sat, Jan 26, 2013 Morrie car insurance quotes nj 348 auto insurance %-]]] propecia generic name %-[ DOT 348 DOT %-]]] DOT %-[

Thu, Jan 24, 2013 Candid cheap california auto insurance 1258 cheap auto insurance =-]] car insurance 669509 DOT 1258 DOT =-]] DOT 669509

Wed, Jan 16, 2013 Lorene auto insurance quotes 65801 cheap auto insurance qsooyu car insurance 88298 DOT 65801 DOT qsooyu DOT 88298

Wed, Jun 6, 2012 Des United States

The optional parameter makes no sense to me at all! You don't need to overload these two functions (at least in the example). In fact - another poster said this only creates an opportunity for bugs - and I think this is a HUGE opportunity because it is simply not clear which function will be called. Perhaps if there was an example of when this "feature" would make sense...

Thu, May 10, 2012

I don't understand the point behind the optional overloading, unless this somehow works if there is a third method z involved. If I am going to call f(1) then presumably x will be called... but if I have a single parameter overload, where is the use case for making a method with 2 parameters where the second one is optional? Clearly if I only use one of them my method with 2 params won't be called? This just seems like an excellent opportunity to introduce bugs in your code.

Thu, May 3, 2012 Richard

@Andrew: If it follows the same rules as C# 4, it will bind to the first overload.

Methods with all parameters specified are considered first. If there is a match, that method is used. If not, then methods with optional parameters are considered. If there is precisely one candidate method, that method will be used. If there is more than one candidate, you'll get a compiler error.

Of course, the VB team might decide to do something different just to confuse us!

Thu, May 3, 2012 Andrew

So if we have Private Sub f(x As Integer) and Private Sub f(x As Integer, Optional y As Integer = 0) and refer to f(1), which function is called?

Sat, Apr 28, 2012 Joe Kunk Okemos MI USA

Richard, thank you for reading and commenting on this article. The intent of this article was to point out lesser known improvements in Visual Basic 11 that can be enjoyed by the Visual Basic developer. I did not mean to imply that they were only improvements unique to Visual Basic. I apologize if that was unclear. In fact, under the principle of co-evolution, improvements that appear in only C# or VB will become increasingly rare, which I consider a good thing.

Tue, Apr 24, 2012 Richard

Most of these are improvements in the framework or in Visual Studio, not specifically in VB.

"Optional Parameter Overloads" and "Type Inference for Return Array Literals" are VB-specific.

"Compiler Switches" *might* be VB-specific, but they're likely to be added to C# as well.

"Bug Fixes" might have some VB-specific fixes, but mostly affect Visual Studio.

"Async", "Read-Only Types" and the untitled "Security" section are improvements to the framework; there's nothing VB-specific about them.

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.