views:

150

answers:

3

In VB.NET, it is entirely possible to pass an integer as a string parameter to a method without calling .ToString() - it's even possible to call .ToString without the ()'s. The code will run without a problem, VB will interpret the integer as a string without having been told to.

In C#, these would cause compilation errors - you are required to call .ToString() and to call it correctly in that situation before it will compile.

Is there a way to make the VB compilation process check for the same things as the C# compilation process? Would it be best practice in a mixed team to force this check?

+3  A: 

Make sure you have

Option Strict On
Option Explicit On
Option Infer On

in your project settings.

Other than that, not much can be done. VB.NET is a different language and has different kind of restrictions from C#. If you want to make it exactly like C#, why wouldn't you simply switch to C#?

Mehrdad Afshari
In my specific case, I joined a team that uses VB.NET. All new projects are created in VB.NET for consistency. But the question came up recently as we were discussing differences, and I was curious.
pete the pagan-gerbil
@pete: I’m a huge fan of VB, and of consistency. But isn’t one of the big selling points of .NET that different projects, written in different languages (i.e. C# and VB) can work together seamlessly?
Konrad Rudolph
@Konrad: yes, but if your team lead prefers VB, and the only other developer hasn't used C# before... it was explained as "anyone can pick up any project and get to work on it."Personally, I can flip between the two without a problem, but I can see it would be easier for some people not to have to mentally change gears when they open a different project.
pete the pagan-gerbil
+8  A: 

There are several compiler options you can switch on, Option Strict will do most of what you want i.e. will not allow you to pass an integer for a string.
You set it on the Compile tab for your projects settings, or you can just put Option Strict On at the top of a class/module file.

However things like being able to call o.ToString instead of o.ToString() are part of the language semantics, you can't do anything about that.

If you REALLY want something that compiles just like C#, then you have to use C#.
Sorry :(

Hope this helps

Binary Worrier
+2  A: 

Enabling Option Explicit and Option Strict will make VB as strict as C#. Enabling these options in your Visual Studio options is highly recommended (so that they will be enabled for every project). In fact, I recommend to never disable these options, except perhaps on a per-file basis when dealing with COM interop (PIA) where late binding can really make the code more concise.

But as others have said, C# and VB are different languages and this is especially evident in their syntax – so omitting the parentheses after a certain method calls will always be possible, just like C# will always require semi-colons at the end of their statements while VB doesn’t allow them.

Konrad Rudolph