VB.NET for C# Programmers
October 12th, 2004On a platform where you can choose between two distinct languages to write software you will eventually need to know how both work. On this recent assignment they only use VB.NET and will not use C#. I had to learn, but all in all it is not such a daunting task. For starters you are still using the same .NET API you are familiar with in C#. In fact, what you are generally going to be coding against is objects and frameworks which were present in classic VB which were simply modernized to run in the CLR. C# is really just glommed onto that legacy with some syntax sugar. But while the majority of the coding is pretty much the same with minor syntax differences, there are a few caveats to consider.
First is the default namespace. When you create a project in VB.NET it will default the namespace to the name of the project. If you do not know this it will cause you headaches as you will not know how to use (or import) classes from other assemblies. It will actually prefix all your namespaces with the project name. To stop that, simply edit the project properties and clear out the value of the default namespace.
Another assumption I had to overcome is that when I use an if statement the first boolean result would short-circuit the second expression from being evaluated. In VB.NET that is not the case, so you want to use the OrElse and AndAlso statements to force the short-circuit behavior.
You can also be a little sloppy in VB.NET. In Visual Studio the code will auto-indent as you move from line to line and also adjust the case of variable names and statements for you on the fly. Sometimes that gets in the way because sometimes you want it to be case sensitive. So you can make VB.NET more strict by adding Option Strict On above your class declaration. You can also update the project build setting to enable strict syntax for all code in a given project. Then Visual Studio will be sufficiently terse to help prevent simple errors.
There were a few other minor headaches I came across with VB.NET but after a week it seems I am getting a good feel for it. But I will want to get back to C# or Java.
