Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no error in VB.NET

Tags:

vb.net

I have recently switched from a c# team to a vb.net team. One of the things I have not been able to find an answer to is the differences in compile error / options. Let me explain.

In C# i will, using default settings, get a compile time error when trying to pass in an invalid type to a templated class like below. Here I create an Animal with a string type and afterwards I pass in a datetime which results in an compile error.

 IAnimal<string> animal = new Animal<string>();
 animal.SetTrainer(DateTime.Now);

I know I will get the same compile time error in vb.net with "Option Strict". There is, however, a lot of legacy (VB) code in the same file that will not compile with "Option Strict". What options do I have. Im thinking this:

  1. Switch to "Option Strict" and fix all errors. Will take some time and may break working code.
  2. Maybe there is an alternate that will ensure compile time check of generics. After all generics are rather new so maybe there is a way of always enforcing this.
  3. ?

Thanks in advance

like image 513
Flodpanter Avatar asked Oct 24 '12 07:10

Flodpanter


2 Answers

Double click your Project -> My Project.

Goto Compile and look for Warningconfiguration

Now you can change some settings.

  • Implicit cast
  • Late Binding

don't make them errors but warnings.

That won't make compile time errors but you can at least see some warnings.

Another solution would be to make your class a partial class and move your code to a new file. You can set Option Strict / Option Explicit on a per file basis.

like image 107
Jürgen Steinblock Avatar answered Sep 20 '22 01:09

Jürgen Steinblock


Switch to "Option Strict" and fix all errors. Will take some time and may break working code.

Yes, do that. It will help you remain sanity.

Most errors that will pop up are probably simple casting issues, which are easy to fix (a CInt here, a ToString() there...).

You don't have to fix your whole solution or project at once, since you can enable Option Strict On at file level. Make it a good habbit to fix every file as you have to touch it.

This will not always be possibly, but you can also just move code that heavily relies on Option Strict On (e.g. COM stuff) to another file without breaking changes.

like image 28
sloth Avatar answered Sep 21 '22 01:09

sloth