Visual Basic 11 Beta Available for Download!

As you may have read in Jason’s announcement, today we announced that Visual Studio 11 Beta is now available for download. This is a hugerelease for Visual Basic as it provides full platform support for building Windows 8 Metro style apps, a first-class asynchronous programming experience, and also the long-awaited Iterators feature. And in the true spirit of VB, you’ll see a ton of other improvements that will make you more productive every day.

AsyncAs the world moves to mobile phones and tablets, the demand for responsiveness in today’s applications is higher than ever. Things like database queries, network requests, and disk access all have potential to block the UI and leave users frustrated. While user expectations continue to climb, the tool/platform support for making asynchronous programming easy hasn’t kept pace, until now. With the new Async/Await keywords, VB11 makes asynchronous programming really simple:

The Await keyword kicks off an asynchronous request without blocking the UI. The function returns a Task(Of T) at the point of the Await expression, but this is just a placeholder for the return value that will come from GetFileAsync. Once that work completes, the method resumes and the variable packagedFile is assigned to.

VB11 also includes full async debugging support. F10-Step-Over (or Shift+F8 on VB Profile) now does what you’d expect. If you’re still on the function-declaration-line then it steps out to the caller. But often (e.g. if you’ve gone past an await) then the concept of “caller” doesn’t even exist. So, for consistency, if you’re anywhere outside the declaration line then Shift+F11 (Ctrl+Shift+F8 on VB Profile) will step out to someone who’s awaiting you.

The other thing we’ve added is async unit-testing support in MSTest. xUnit now supports this as well.

<TestMethod>

AsyncFunction Test1() AsTask

Dim x = Await Engine.GetSevenAsync()

Assert.AreEqual(x, 6)

EndFunction

<Xunit.Fact>

AsyncFunction Test2() AsTask

Dim x = Await Engine.GetSevenAsync()

Xunit.Assert.Equal(x, 6)

EndFunction

IteratorsIterators are a new feature in VB11 that make it easier to walk through collections such as lists and arrays. Each element is returned to the calling method immediately, before the next element in the sequence is accessed.

In addition to working with collections, you can use iterators to write your own custom LINQ query operators. For instance, the following example prints out only the even numbers in the array:

ModuleModule1

Sub Main()

Dim query = From n In {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Where n Mod 2 = 0

Select n

ForEach item In query

Console.WriteLine(item)

Next

Console.ReadLine()

EndSub

<System.Runtime.CompilerServices.Extension>

IteratorFunction Where(source AsInteger(),

predicate AsFunc(OfInteger, Boolean)

) AsIEnumerable(OfInteger)

ForEach num In source

If predicate(num) Then

Console.WriteLine("Yielding " & num)

Yield num

EndIf

Next

EndFunction

EndModule

Note the new Iterator and Yield keywords in the Where function. (The LINQ query binds to this Where extension method since it’s a better match than the Where(Of T) operator defined in the standard query operators).

VB also allows iterator lambdas! In this example, we use an iterator in an expression context and combine it in a powerful way with XML Literals (note that we’re effectively embedding statements inside embedded expressions now):

Dim images =

<html>

<body>

<%=

IteratorFunction()

ForEach fn In IO.Directory.EnumerateFiles("c:\\", "*.jpg")

Yield<imgsrc=<%= fn %>></img>

Next

EndFunction.Invoke() %>

</body>

</html>

Namespace GlobalVB has always had the Namespace and Global keywords, but now you can use them together!

NamespaceGlobal

NamespaceGlobal.<NamespaceName>

This gives you a lot more flexibility around which namespace your code ends up in, and is particularly useful for code-generation scenarios. For a full description of the feature, check out Lucian’s excellent post here.

Optional Parameters in Overloaded MethodsPreviously, overloads of a method were not permitted if the only difference between them was optional parameters, so the following code would be invalid:

Sub f(x AsInteger)

EndSub

Sub f(x AsInteger, Optional y AsInteger = 0)

EndSub

In VB11 this code is now valid, which gives you more flexibility and improves the ability to version methods (though there’s still more we need to do in this area).

Limitless (Command-line) Errors!This is actually a good thing, let me explain. For performance reasons, the Visual Basic IDE maxes out at 101 errors (with error #102 being “Maximum number of errors exceeded.”) This can make it difficult to estimate the amount of work remaining in certain situations, particularly in upgrade scenarios. We have removed this limit from the command-line compiler in this release, though it still there in the IDE. What this means is if you want to know exactly how many errors there are for a project, just invoke the compiler through msbuild.exe or vbc.exe and you’ll get your answer.

Caller Info AttributesThe compiler now recognizes three special attributes: <CallerMemberName>, <CallerLineNumber>, and <CallerFilePath>. This is great for logging scenarios, and allows you to have the name, line number, and/or file path of the invoking method passed into the logging function as optional parameters.

Another great use case for this is when implementing INotifyPropertyChanging:

Simplified Code Spit (a.k.a. No More ByVal!)The IDE will no longer insert “ByVal” in method signatures unless you explicitly type it in. This reduces a lot of the visual noise in method declarations and makes them more readable. Also, the IDE will no longer insert the fully-qualified name for a type (such as “System.Object” or “System.EventArgs”) when the applicable imports are already in scope. (Because in this case “System” is already a project-level import).

This also works automatically for all interface methods generated when you press enter after an Implements clause.

View Call HierarchyIn VB11, if you right-click a method name, you’ll see a new option for “View Call Hierarchy” that brings up a window that looks like this:

(Click on the image to see a more detailed view)

It shows you all calls to/from that method, and also if it gets overridden anywhere. You can then walk the tree and understand how these methods are used throughout your codebase. For a more detailed overview, check out this post.

Getting StartedHopefully that gives you a taste of what’s new in VB11 (and we haven’t even touched on all the performance improvements!). There are far more enhancements than we can cover here, so watch for a future post that goes into more detail. For next steps:

1.Download the Bits!Download Visual Studio 11 Beta (you can also get Team Foundation Server 11 Beta, and .NET Framework 4.5 Beta from here). Visual Studio 11 Beta can be installed on Windows 7, or you can install it on top of the Windows 8 Consumer Preview.

3.Send us Feedback!The forums are available for questions, both in Visual Studio and Windows 8. If you find a bug please let us know through the Microsoft Connect site. For feature suggestions check out the UserVoice site (which also allows you to vote on other users’ feature ideas).

We hope you enjoy the product and look forward to seeing tons of great VB apps!

I'm not sure if the await example makes sense: TextBlock1.Text is set *after* the await line, which means it is part of the continuation. Thus, "Retrieving File..." is printed only *after* the file has been downloaded completely...

Héctor

1 Mar 2012 8:33 AM

Why the new Iterator keyword? is it because you found a lot of/important customers with problems because they used "yield" as a variable name in their codebases? Or is it because of something else?

Are the arguments decorated with the new Caller Info attributes visible to developers? or only inside the scope of the method? It would be nice if it would be the second.

PS: Sorry if this appears as a double comment, I don't know if my previous one has been submitted.

ted

2 Mar 2012 8:13 AM

Good going. Delegates and background threads finally simplified.

Add in a WPF databinding check method like BOOL Me.Property.EnsuredIsBoundToXaml() and we're set

While the Iterator keyword definitely allowed us to avoid the problem you mention of introducing a breaking change to existing source bases which use Yield as an identifier (e.g. financial applications) and to keep a concise keyword like Yield instead of a keyword pair such as Yield Return that wasn't the only reason. VB, in general has a declarative style to it (XML literals, Handles, Implements); we felt that it was important in VB that if a function was an iterator that the code simply state that. Similarly if people were going to call them Iterator and Async Functions then the code should as well. You can see this pattern in other VB modifiers such as ReadOnly and WriteOnly properties - we could infer that the Setter was missing but semantically that means ReadOnly so the property is declared ReadOnly.

Another benefit to the explicit declarative modifier is that we can give a better editor experience. In VB when you type "Iterator Function F As Integer" the IDE will automatically insert the "IEnumerable(Of Integer)" for you. Similarly if you type "Async Function F As Integer" the IDE will replace the "As Integer" with "As Task(Of Integer)" so that you don't have to. Again, parallels can be drawn with Properties where the presence of the ReadOnly modifier allows us to auto-generate better code for you.

As for the CallerInfo attributes they don't hide the parameters from consumers of the method, no. These parameters look just like normal optional parameters. This is important as an example for a method which has multiple overloads which take CallerInfo parameters. One overload should explicitly pass its arguments to other overloads to preserve the line CallerInfo of an external caller. Otherwise the innermost overload would always get the CallerInfo of whatever other Overload called it. We're looking at ways to note these parameters as more special though such as displaying "(caller name)" or the current line number as the default value instead of the one declared in source to make it clearer to developers their true nature.

Regards,

Anthony D. Green

Program Manager

Visual Basic & C# Languages Team

JC

6 Mar 2012 7:11 AM

I would like to see nested subprocedures. A sub or function that is declared completely within the context of another sub and could only be called from within that parent sub. The parent sub's variables would be available to the child. There are so many times when I want to call a new sub that will only be called from one sub and I have to pass lots of parms to the new sub. Nested subs would allow me to write the new sub inside the parent without having to pass parms. Delphi has had this for years!

Tshepo

7 Mar 2012 11:46 PM

I would like to get notes about the classes in VB.net. I am still new in programming. I can be contacted at tshepomiya@gmail.com

Roger Keulen

19 Mar 2012 5:25 AM

I would like to see nested subprocedures ! (Me to!)

@JC: Delphi is a real-oop language....

Class Test

Public Sub New()

End Sub

Public Sub New(Param as string)

.......

End Sub

End class

Class NotOOP

inherits Test

Public Sub New()

End Sub

End class

So: Bla as new NotOOP("Delphi is for nerds, VB is just what it is..... Basic stuff")

Asbjørn Hansen

25 Mar 2012 3:21 PM

Jeg har besvær med at Downloade Visual Basic 11 Beta.

Héctor

30 Mar 2012 1:58 AM

Although declarative style is often nice, it may also make the language a bit more verbose, which is a problem for some people out there.

Also, on the subject of properties, when you make an interface with a readonly property, C# allows its implementation to have a private set, however in VB you are forced to use ReadOnly, which may be correct at some levels, but on others it makes you add some more code than desired. I hope this won't cause similar undesired behaviour.

Joaquim

30 Mar 2012 2:20 PM

The Real Transparency and Opacy, on controls, stills not be possible?

Filip

3 Apr 2012 6:35 PM

How about making Option Strict On as default? I'm sure that would please many dedicated VB programmers as well as myself. And as Joaquim mentioned, Opacity property on controls would be very nice.

Héctor

8 Apr 2012 2:52 PM

Opacity on controls is a Windows Forms issue, and it's unlikely we'll see any new update for it ever... a shame, because I still prefer Windows Forms over WPF when making a desktop app, but if it was already placed aside by Microsoft when WPF made its debut, now with Metro we can completely forget about it.

Joaquim

18 Apr 2012 1:14 PM

sorry, but what is the language limitation!?! is our imagination;)(is what everyone learn)

then why not, just, update the Transparency property(make it more faster) and with these way we can do the Opacy too(they continue be a false Transparency\Opacy, but better use it more faster than nothing);)