CodeHappy

August 5, 2008

Visual Basic 2008 is phenomenally powerful

Filed under: Tech Opinion — pwrighta @ 5:00 pm

The C# advocates have done a great job at making .NET development synonymous with that language. Indeed, most online code examples you’ll find for .NET these days are C# based, as are most of the features you’ll see on sites like Channel9. Visual Basic is not dead yet though, and in fact Visual Basic 2008 is probably the most powerful version of Microsoft’s first RAD language ever.

I started looking through the VB docs last night after seeing an interview on Channel9 with Amanda Silva at TechEd. Did you know that XML can now be used as a first class literal in Visual Basic? Check this out

Dim author = _
    <Author>
        <Name>Pete Wright</Name>
    </Author>

If you’re particularly eagle eyed you may also have noticed in that snippet that I didn’t declare a type for ‘author’. That’s another feature new in 2008 – Type Inference. Since the XML isn’t formatted as a full document, the type of ‘author’ is XElement. Adding a doctype to the fragment makes the type an XDocument at runtime.

Dim author = _
    <?xml version="1.0"?>
    <Author>
        <Name>Pete Wright</Name>
    </Author>

You can’t do that in C# at all (although you could get close with a nasty multi-line string and then various casting and object instantiationtricks). VB2008 even lets you embed queries right inside the XML literal, making it almost border on a built in XML templating system right there in the language. Visual Studio 2008 even throws in a little Intellisense magic when working with XML literals to make the whole thing just feel oh so right as well.

VB2008 takes a leaf out of the Python book with the use of object initializers. Imagine a situation where you want to create an object and immediately set a bunch of properties on that object. With an object initializer this can be achieved in a single command

Dim author = New Author With { .Name = "Pete Wright", .Genre = "Non Fiction"}

This creates an instance of a class called Author and then right away assigns values to the Name and Genre properties on the object. You can take this even further with the use of anonymous types. Check this out.

Dim author = New { .Name = "Pete Wright", .Genre = "Non Fiction"}

This makes VB create a brand new type on the fly with two properties (Name and Genre) and then sets those properties on the resulting instance. To me that’s wonderfully reminiscent of similar features in Python and Ruby and adds a stunning amount of power and flexibility to the old workhorse. Again, in both examples above you can see Type Inference at work. Why on earth would you want to specify the type when the immediate assignment makes it crystal clear just what we’re working with.

Type Inference, initializers, anonymous types and XML literal support are just a few of the new features I stumbled across last night, but there’s a lot more. VB has support for Lambdas now, as well as extension methods that let you extend objects at runtime in a Ruby Mixin kind of way. Linq adds powerful querying support irrespective of the data source you’re working with.

I’m going to start dusting off old code samples I wrote and updating them to learn more about the new features. I’ll probably blog the results as well. I’ve always had a soft spot for Visual Basic, if you couldn’t tell from the number of books I’ve written on it, and VB2008 has really ignited the old passion in me once again. While there are plenty of people out there that will happily knock you for using VB, the simple fact of the matter is that it is a phenomenally powerful programming language. It’s verbosity also makes it an ideal counter to the terseness of C# for many. That verbosity, used correctly, so often equates to cleaner, more instantly readable code.

Long Live VB I say, and kudos to Microsoft for giving the language a nice modern shot in the arm.

7 Comments »

  1. umm…don’t they all run on the CLR? How would anything VB can do be functionally better than C# or any other CLR language?

    OK, I will give you the With…End With construct, it rocks and C# really suffers without it…but are you saying that you can’t do the things you are talking about in C#?

    I personally prefer the VB syntax over C#, probably because I came into programming via BASIC, QuickBASIC, then VB and I find many of the holdovers from C++ that show up mindlessly in C# annoying. I am just not sure that your examples represent something that VB offers that C# doesn’t.

    Comment by M@ — August 12, 2008 @ 2:53 pm | Reply

  2. No no, not at all. There are language constructs in C# that don’t have an equivalent in VB, VB has always had language constructs that C# doesn’t (optional parameters anyone). What I’m saying is that Microsoft have really done a good job extending the language itself in the 2008 release by adding in support for things like Lambdas, anonymous types, XML literals and so on.

    VB has always had it’s detractors but each release that comes out of Microsoft moves the language further and further away from all the things people would typically want to point fingers and sneer at. This latest release continues that trend adding some awesome new features that I completely love.

    Comment by pwrighta — August 12, 2008 @ 7:16 pm | Reply

  3. God.. nice to see the code snippets completely forked up.

    Comment by pwrighta — August 12, 2008 @ 7:16 pm | Reply

  4. I’m still trying to learn VB at this point for an impending and necessary career change. Is learning VB2008 a better idea than continuing forward with an older iteration? And how would you suggest tackling a language like Ruby or Python for a first-timer, non-programmer trying to break into the industry?

    Love the blog, by the way.

    Comment by DarcKnyt — August 13, 2008 @ 9:56 am | Reply

  5. Wow – answering that question is a flamewar waiting to happen. I’ll try my best though.

    First up, whether to learn VB2008 as opposed to an older version. Well, 2008 is very new and so there are unlikely to be all that many people out there looking to hire VB2008 developers at the moment, particularly inexperienced ones. That said, if you are learning with a view to changing career then it’s probably a good place to start since in 6 to 12 months time the hiring situation may have changed.

    In terms of Ruby or Python – your best bet is probably to grab a couple of books and dig in. If you’re looking at Ruby you are probably also looking at Ruby on Rails so you could do a lot worse than to pick up a copy of “Agile Web Development With Rails” from the Pragmatic Programmers. However, you’ll also need a book on Ruby itself to really round out your study, so check out “Programming Ruby”, also from the Pragmatic guys (the first edition of that is actually available for free at their website now, but it’s a little dated). For Python, either read the tutorials at python.org or look at O’Reilly’s Learning Python. Once you have the language down go ahead and pick a web development framework of your choice (Pylons, TurboGears, Django etc) and dive in via each ones website.

    Look at the field you want to enter though and check the jobs out. Despite their popularity, Ruby and Python can’t hold a candle to C#, Visual Basic or Java in terms of available jobs out there. Also, if you considering web development bear in mind that you have a steep road to climb since whatever you choose you’ll need to tackle the programming language, the web development framework, HTML/XHTML, CSS and probably Javascript as well. Not an impossible road by any means, but you shouldn’t be under any illusion that it’s going to be tough (and hopefully fun).

    Comment by pwrighta — August 13, 2008 @ 11:45 am | Reply

  6. Thanks for the response! I really appreciate it. Your advice is sage, and I’ll be very aware of what’s happening as I progress.

    I don’t know if especially developing for the web appeals to me, but it’s something I’d better at least consider going forward; it is, after all, the future (if not the present).

    Thanks for taking the time, I’m grateful!

    Comment by DarcKnyt — August 13, 2008 @ 2:28 pm | Reply

  7. [...] mentioned that I was starting to tinker with Visual Studio 2008 and Visual Basic 2008. Microsoft’s technologies aren’t something I get to use in my day to day work anymore [...]

    Pingback by Visual Studio 2008’s daunting learning curve « CodeHappy — August 13, 2008 @ 11:43 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.