Can you make VB.NET compilation as strict as C #?

In VB.NET, it is possible to pass an integer as a string parameter to a method without calling .ToString() - you can even call .ToString without (). The code will work without problems, VB will interpret the integer as a string without reporting.

In C #, this will lead to compilation errors - you must call .ToString() and call correctly in this situation before it compiles.

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

+4
source share
3 answers

There are several compiler options that you can enable, Option Strict will do most of what you want, i.e. won't let you pass an integer for a string.
You install it on the Compilation tab for your project settings, or you can simply put Option Strict On at the top of the class / module file.

However, things like the ability to call o.ToString instead of o.ToString() are part of the semantics of the language, there is nothing you can do about it.

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

Hope this helps

+9
source

Make sure you have

 Option Strict On Option Explicit On Option Infer On 

in the settings of your project.

In addition, not much can be done. VB.NET is a different language and has different restrictions from C #. If you want to do this just like C #, why don't you just switch to C #?

+3
source

Enabling Option Explicit and Option Strict will make VB as strong as C # . It is recommended that you enable these options in your Visual Studio options (so that they are enabled for each project). In fact, I recommend never disabling these options, except perhaps for every file when working 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 noticeable in their syntax - therefore omitting parentheses after calls to certain methods will always be possible, just as C # will always require half-columns at the end of their statements while VB does not allow them.

+3
source

Source: https://habr.com/ru/post/1303927/


All Articles