Home > .NET > Some Love for VB.NET 10 Too

Some Love for VB.NET 10 Too

Some Love for VB.NET 10 Too

November 24th | 2009

Some Love for VB.NET 10 Too

At Thycotic we typically work with C#. In fact, 90% of the code that we write is written in C#. However, it’s no secret that I am a VB.NET Fanatic. Generally, C# developers don’t look kindly on VB.NET developers because they consider VB.Net an inferior language. But I particularly like how it’s implemented in Visual Studio.

One of the criticisms of VB.NET is that is that some of its features are not up to par with C#. That’s an indisputable fact, I’ll admit. But a lot has been done to the upcoming VB.NET 10 for the .NET Framework 4.0, to bring VB.NET closer to C# for syntactic sugar.

Auto Properties

C# 3.0 introduced a really cool feature called automatic properties. Essentially, the C# compiler emits a backing field for you so you don’t have to do it yourself. This feature will be available in VB.NET 10. Its syntax looks like this:

Public Property MyAutoProperty As Object

That’s it! It’s just that one line; even simpler than C#. Of course you can decorate it with ReadOnly or WriteOnly so that it only has a getter or setter. The one thing it can do that C# 3.0 or 4.0 doesn’t let you do, however, is auto-initialize it.

Public Property MyAutoInteger As Integer = 10

MyAutoInteger will default to 10. The VB.NET compiler will secretly put this in the constructor or static constructor if the property is static, similar to the way fields can be auto-initialized.

Subs as Lambdas

C# 3.0 introduced a rich syntax for Lambda expressions, and VB.NET followed suit to support LINQ. One limitation is that the passed in delegate must be a function that returns a value, and not a sub. Consider the following:

Sub Main()
    DoWork(Sub(x) Console.WriteLine(x))
End Sub

Public Sub DoWork(ByVal work As Action(Of String))
    work("Some String Maybe")
End Sub

This simply wouldn’t compile in VB.NET 9 with Visual Studio 2008 because DoWork expects an Action delegate, which does not return anything thus making it a Sub, not a Function. I’m happy to say this will compile in Visual Studio 2010.

Collection Initializers

If you’ve ever had a List or Dictionary you wanted to quickly initialize with data for something simple like a unit test, C# made this easy with collection initializers. VB.NET 10 introduces a similar syntax to allow initializing a collection.

Dim helloWorld As New List(Of String) From {"Hello", "World"}

This will create a list with “Hello” and “World” in the list. Likewise for a dictionary:

Dim helloWorld As New Dictionary(Of Integer, String) From {{1, "Hello"}, {2, "World"}}

Good stuff!

Line Continuations

VB.NET was criticized for its reliance on line feeds to decide where the next statement block would start. Previously, to continue the statement to the next line you had to use an underscore to indicate that the next line was part of the current one. In VB.NET 10, that remains true. But VB.NET 10 allows flexibility while implicitly understanding where the underscore is. For Example:

Dim someNumber As String = "Hello" &
     "World"

or

Dim someNumber As Integer = 10 +
     40

Better COM Support & Indexed Properties

C# 4.0 introduced better handling for “missing” arguments in COM. VB.NET has had that for a while.

C# 4.0 has partial support for indexed properties to complete their COM enhancements. Sadly, C# only lets you consume them from COM, not declare them, which VB.NET has been able to do since its inception.

And let’s not forget that VB.NET has plenty of other language tools that C# lacks such as the “With” construct:

Dim builder = New StringBuilder()
With builder
    .Append("The ")
    .Append("Quick ")
    .Append("Brown ")
    .Append("Fox ")
    .Append("Jumped ")
    .Append("Over ")
    .Append("The ")
    .Append("Lazy ")
    .Append("Dog.")
End With

A contrived example, sure, but you get the idea of its usefulness. The “Handles” as a way of handling events is one of my favorite features. Of course C# developers will quickly to tell you that it’s a horrible syntax.

Conclusion

Both languages definitely have their advantages and disadvantages. Clearly though, VB.NET 10 has a lot of polish on it that cannot be ignored.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

Categories: .NET
  1. November 25, 2009 at 6:32 am

    Regarding Type inference, VB 9 will correctly identify someNumber as an integer when it is declared as follows:

    Dim someNumber = 10

    I’m definately looking forward to subs as lambdas.

  2. Richard
    November 25, 2009 at 12:24 pm

    Indexed properties are a horrible idea. C# is getting support for consuming them simply because so many legacy COM servers use them. It’s quite simple to produce a clean C# solution that looks like indexed properties from the consumer’s point of view.

    The “Handles” clause is also horrible. I’ve lost count of the number of times VB ASP.NET developers have posted questions about why their event handlers are being fired three times, usually because they have the handler wired up three times – the markup, the “Handles” clause in the code-behind, and AutoEventWireup=”True” (the default).

    The “Implements” clause is another horrible idea. It should only be required for explicit interface implementations; if an interface exposes a Count property and my class implements the interface and exposes a public Count property, why should I have to tell the compiler that my property implements the interface property?

    For that matter, why should I have to tell the compiler that a regular property without a Set accessor is ReadOnly, or a property without a Get is WriteOnly? It’s not even a warning, it’s a compiler error! How stupid do the VB compiler team think VB developers are?

    Don’t event get me started about the AndAlso/OrElse operators, or array declarations using the upper bound rather than the array size!

    VB.NET 10 may have some nice improvements over the previous versions, but I’m still glad I switched to C# back when .NET v1.1 came out.

  3. K. Jones
    November 25, 2009 at 5:40 pm

    @Darragh – You are absolutely correct. This was a feature that was introduced into VB.NET 9, not VB.NET 10. I’ve had the section removed to avoid confusion. Thanks!

  4. K. Jones
    November 25, 2009 at 5:50 pm

    @Richard,

    Thanks for your feedback. I know VB.NET isn’t for everyone, but I happen to appreciate most of those features.

    Indexed Properties – Yes I think it should be avoided. However the VB.NET team had to support this feature as VB 6 had it. I don’t think it’s a horrible idea because it makes working with legacy VB6 and COM applications easier. I agree, it may not be the best idea to use it but it certainly made backwards compatibility easier, and also why C# 4.0 is getting it as well.

    Implements – I agree.

    Handles – This is just syntactic sugar. You can have the same problem in C# with AutoEventWireup=true but they were still specifically handling the Page.Load using event subscription. It’s no different than calling AddHandler in the constructor, or using += from C#.

    ReadOnly / WriteOnly – This is for better supported with interfaces. Since VB.NET does not implement “stub” getters and setters like C# does, the ReadOnly and WriteOnly was how it was decided to be done.

    I’m not trying to start a debate, but all of the features have their history and reasons behind all of them. And for the most part, if you don’t like the feature then don’t use it. There are still some quirks to it but I also happen to think Visual Studio’s support for VB.NET is better than C#, assuming you aren’t using a tool like CodeSmith or Resharper.

  5. December 5, 2009 at 6:14 pm

    Hey Kevin,

    Really cool blog. I really enjoyed reading it. You have really highlighted the cool features of the VB.NET.

  6. March 18, 2010 at 10:08 am

    I also love VB.Net 🙂

  7. January 20, 2011 at 5:37 pm

    I hate it because i dont have any girlfriend

  8. Mugambo
    February 9, 2011 at 2:06 pm

    Richard

    your following statement, who do you think is at fault??? I don’t see it as VB issue…

    The “Handles” clause is also horrible. I’ve lost count of the number of times VB ASP.NET developers have posted questions about why their event handlers are being fired three times, usually because they have the handler wired up three times

  9. April 11, 2011 at 10:05 am

    =)

  1. November 24, 2009 at 6:01 pm
  2. December 1, 2009 at 5:04 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: