4

I have to take over a project written in vb.net, which contains more than 400k lines of code written in option strict off mode. I want to build it under option strict on first before I do anything else -- which maybe converting it into C#. I found there's thousands of lines of code raises compilation error, mostly are about implicit type casts.

Is there any tool would help to make it compile under option strict on mode if I don't want to correct every single line manually? Because it's really painful to add CStr/CInt invocation into every line of Code myself.

deerchao
  • 10,454
  • 9
  • 55
  • 60
  • 3
    Why would anyone want to convert all that production code to C#? I tell you I hate Java, but if I inherited a Java program, I would not convert it into VB.NET or any other language. Deal with it. – AMissico Mar 28 '10 at 12:11
  • What I want is to convert bad codes into good ones. Even if I have to stuck with VB.net and don't convert it into C#, I still want it's option-strict-on. – deerchao Mar 28 '10 at 15:45
  • After a while of research, I've decided to add option strict on into one file at a time, and make it compile untill all files are converted. Really a pity I can't find any tools to make this easier. – deerchao Apr 06 '10 at 01:48
  • 7.5 years later and I'm dealing with the same thing: https://stackoverflow.com/questions/47562402/make-compilers-implicit-data-type-conversions-explicit Just seeing this now.. – Isaac Kleinman Dec 06 '17 at 16:36

3 Answers3

4

Just adding CInt or CStr, or Convert.ToString for that matter doesn't gain you anything. you need to look at the design on an individual case by case basis and figure out why the datatypes are mismatched from the ground up.

JohnnyJP
  • 1,216
  • 9
  • 18
2

How about mix and match just to get you running. This is what I think you should do. Set project setting to "Option strict On" All individual .vb files that have a compile error... put "Option strict Off" as the first line in that file. (this will override the project wide setting)

You now have a compiling project.

Next job is when you've got time pick an individual file (search for "Option strict Off") (or when you modify a file for another reason) Remove the "Option strict Off" from the individual file and let VisualStudio auto correct tags fix the errors for you.

MikeG
  • 1,069
  • 1
  • 9
  • 19
0

So you want to both use Option Strict and to not-use it at the same time?

I'll tell you what: first turn Option Strict to On, then under Warning Configurations change the "Implicit Conversion" condition from "Error" to "Warning" (or "None"). You will then see that the configuration says: Option Strict - "Custom".

But don't fool yourself: this custom setup isn't Strict, as it allows Implicit things to take place. You need to ask yourself: "Why do i want to compile this under option Strict?".

M.A. Hanin
  • 8,044
  • 33
  • 51
  • No, I don't want to fool myself. I want it become real Option Strict code, but not manually -- adding CInt, CStr to every implict tpe conversion. – deerchao Mar 28 '10 at 15:43
  • 1
    It should be done manually, because otherwise it'll just be a fake strict code. All narrowing conversions must be checked manually to ensure they will not throw runtime errors. – M.A. Hanin Mar 28 '10 at 16:13
  • I just want what the VB compiler has done to make it work exprssed explicitly in the code. Guess that's too hard.. – deerchao Apr 23 '10 at 04:17
  • @deerchao but why would you want that? I understand that you're interested in answers and not in discussing the reasons why, but still, it looks like we collectively fail to understand the reasoning behind this. There is a way to make vb.net decide for you - and that's Option Strict Off. We want to help you, but in order for us to be able to help you, we need to understand. – magma Apr 13 '11 at 20:09
  • @magma: The SharpDevelop converter requires better type information to produce C# code, as implicit conversions are by default an error, rather than a warning in VB. So when you convert a body of VB code, it is nice to use some kind of cast before running the conversion; settling on a single language in a large project can be helpful. Less language tax. – dwerner Apr 19 '11 at 16:56
  • @serotonin: did you try http://www.developerfusion.com/tools/convert/vb-to-csharp/ ? – magma Apr 20 '11 at 15:24