Score one for VB

Every now and then (much less now that .NET languages have been around are are pretty mature) I see people who are moving into .NET programming and they ask “which is better, C# or VB.NET.”  Generally speaking there is no “better” but there are some things available in one language but not the other.  Typically I’ve always thought that C# had just a little more – it has the ability to support unsafe code, which I like and use occasionally.  I could never come up with something VB had that C# didn’t.  Until today.

A friend asked me how he could use the Contains() method of a string inside a case statement, and it reminded me of an old VB 6 construct that I’d used, so I tried it to be sure VB.NET still supported it, and sure enough, it works fine:


        Dim myvar As String = "My Test String"

        Select Case True
            Case myvar.Contains("not there")
                Debug.WriteLine("Contains 'not there'")
            Case myvar.Contains("Test")
                Debug.WriteLine("Contains 'Test'")
            Case myvar.Contains("Other")
                Debug.WriteLine("Contains 'Other'")
        End Select

However the construct won’t work in C#.  It won’t even compile because C# expects case labels to be constants.


        string myvar = "My Test String";

        switch (true)
        {
          case myvar.Contains("not there"):
            Debug.WriteLine("Contains 'not there'");
            break;
          case myvar.Contains("Test"):
            Debug.WriteLine("Contains 'Test'");
            break;
          case myvar.Contains("Other"):
            Debug.WriteLine("Contains 'Other'");
            break;
        }

So there you go VB lovers – score on point for your side.  I’m not saying that I’m going to start writing all my code in VB now (not that I have anything against VB, I mean I did co-author a book on it, I’m just really rusty) but here’s some fodder for what some consider a religious debate.

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