Find out what the new year holds for Visual Basic programmers
Microsoft’s Visual Basic 2008, also known as VB 9, is the third version to be based on the .Net Framework.
An added bonus is that the Express variant is free, unrestricted, and co-exists with earlier versions. With .Net established, Microsoft is evolving the language with important changes.
Perhaps the first thing to get your head round is type inference. The idea is that typing Dim i As Integer = 3 is a waste of space.
The compiler can see that 3 is an Integer, so it can infer the type. In VB 2008, if you type Dim i = 3 then the variable will be assigned the Integer type. This is at compile-time, not at runtime, so it is the same as strong typing.
Type inference only applies to local variables, and only when declared and assigned in the same statement. It could break existing code, if you were relying on VB to declare untyped variables as objects, but such errors are easy to fix.
Another space saver is called relaxed event handlers. In VB 2005, every event handler has arguments for sender and EventArgs, like this: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
In VB 2008, you can have this instead: Private Sub Button1_Click() Handles
Button1.Click
It is another step towards uncluttered code, which is a good thing.
Other new language features include Object Initializers – which means you can create an object and set its properties in the same statement – as well as Anonymous types, Lambda expressions, and Extension methods.
Although interesting, they are also part of the plumbing behind the headline feature, Linq (Language Integrated Query). Linq brings database queries into the language, so there’s no need to assemble SQL strings.
Use XML literals
New in VB 2008 is native XML. This looks strange, as it breaks assumptions about
how code is separated from data. This is now valid VB 9 code:
Dim theItems = _
<posts>
<myitem title=”First item” desc=”Some text” link=”http://someurl”/>
</posts>
The above code uses type inference to create a variable of type XDocument. It is interesting, but not yet useful. The value becomes apparent when you combine it with another feature called embedded expressions. Here’s how you can use this to transform the XML snippet above to a valid RSS feed.
First, start a new project in the free Visual Web Developer Express. Add a new page called blogfeed.aspx. We will be generating the content in code, so delete everything in blogfeed.aspx except the statement declaring the Page.
Now open the code-behind file and write a GenerateFeed function, returning a string. The first part of the function is the snippet of code above, creating the myXml variable, to which I added a couple of additional myitem elements. Next, declare a second XDocument variable, pasting the XML from an existing RSS feed. I used one from a WordPress blog.
I did not paste the entire feed, but stripped it down so that it only includes one item element. I also removed some non-essential elements for the sake of brevity. This is now a template for the new feed, as shown at the end of this article.
Lurking in the middle of this block of XML is some code. The first expression
comes in the pubDate element:
<%= Now.ToString(“r”) %>
Using the same notation as in old-style ASP, this evaluates a VB function and inserts the result into the pubDate element.
The next block is more interesting. It uses a Linq to XML query to retrieve all the myitem elements in the first XDocument variable, called theItems.
There is a useful operator called a descendant axis property, represented by three dots (...). This returns all the elements of the given name wherever they are in the XML tree, starting from the specified element.
Further expressions retrieve the attribute values of each myitem, using the @ attribute operator, and insert them into the XML.
There are a couple of further steps before the code will run. VB will not
compile the code if the namespaces are unrecognised. Fix this by adding imports
statements for the namespaces, at the top of the code-behind file:
Imports <xmlns:content=”http://purl.org/rss/1.0/4
modules/content/”>
Imports <xmlns:dc=”http://purl.org/dc/elements/1.1/”>
Finally, add code for the Page Load event:
Response.Write(GenerateFeed)
Run the code. All being well, blogfeed.aspx now contains a feed that Internet
Explorer or RSS reader recognises.
This simple example demonstrates how to manipulate XML in Visual Basic 2008 and how to transform one type of XML into another, but you could also source the RSS content from a database or elsewhere. You can also get intellisense for specific XML content, by importing or generating a schema.
What’s in Visual Basic 10
Amanda Silver, lead program manager for Visual Basic, told PCW about some of the
plans for VB beyond the just-released 2008 version. Some of these are likely to
come in a service pack, or in an add-in called a Powerpack, while others are
more speculative.
The first thing Silver mentioned is a DataRepeater control. “The next episode in the PowerPack controls is a data repeater control. Remember this from VB 6? The repeater control is a natural fit with Linq, because you end up with a collection, and you can use what comes back from the collection and bind to it directly.”
Next, she mentions how Intellisense for XML will be improved, probably in a service pack, removing the need to create inferred schemas.
Silver also told us about VBX, and how it relates to Microsoft’s Dynamic Language Runtime (DLR). “The DLR is about bringing dynamic languages to the .Net platform. Bringing Ruby and Python to .Net. VB is already a dynamic language, but there are a couple of services that are coming via the DLR that we will be able to take advantage of.
"The way that VB is a dynamic language today is that we can bind at runtime to objects, even when at compile time the type is not known. In the past we have had our own late binder, which is a runtime component that actually does this binding against the metadata that it inspects via reflection. With the DLR w hat’s coming is a set of services that will make that much easier.
"We expect our performance for late binding to improve dramatically, which will take away some of the stigma from late binding that exists today.
“The other thing is that we’ll be able to interoperate between Ruby and Python and Visual Basic. Visual Basic could interoperate with, let’s say, a script that’s executed in Python from Ruby.
“We’re also investigating how we can take the inspiration from Iron Python and Ruby and the interactive console experiences that they offer, and bring some of that back into Visual Basic, possibly even to break out of Visual Studio and have a command-line interface for the VB language, which then makes it interesting for scripting.”
We asked Silver whether anything will be done about the annoying line continuation character in VB.
She said: “People are just used to the fact that you have to put line continuation characters after a parameter or an argument. But with 2008 it has become more annoying again, because we have query expressions that tend to span multiple physical lines but still are part of the same logical line.
"We intend to do something about it for the future. We’re trying to figure out how we can have the line continuation character be implicit and inferred based on the expression that you wrote.”
What about game development with XNA, currently restricted to C# developers? “Unfortunately, there’s nothing I can say on that matter now. I wish I could. XNA is a great product.”
I asked Silver how popular VB was in comparison to the other Express downloads. “Visual Basic is the number one Express download,” she said. “The second place is neck and neck between Web and C++. Then there’s a significant drop-off to the other Express series.”
VB may be losing out to C# in the professional job market, but it shows no sign of losing favour with the generalist programmer.
Resources
Visual Studio Express Downloads
VB Developer Center
New feed template
Dim feed = _
<?xml version=”1.0” encoding=”UTF-8”?>
<rss version=”2.0”
xmlns:content=”http://purl.org/rss/1.0/modules/content/”
xmlns:dc=”http://purl.org/dc/elements/1.1/”
>
<channel>
<title>PCW Blog example</title>
<link>http://someurl</link>
<description>Example blog feed</description>
<pubDate><%= Now.ToString(“r”) %></pubDate>
<language>en</language>
<%= From myitem In theItems...<myitem> _
Select <item>
<title><%= myitem.@title %></title>
<link><%= myitem.@link %></link>
<pubDate><%= Now.ToString(“r”) %></pubDate>
<dc:creator>Your name</dc:creator>
<description><%= myitem.@desc %></description>
</item> %>
</channel>
</rss>
Return feed.ToString()