
So the turning point came when we decided to transfer two projects from VB to C #. Who cares - I ask under the cat.
Introduction
We got these projects from the contracting organization that wrote them for 5-6 years. In total, this is about 1.5 million lines of code, according to the metrics assessment tool in Visual Studio, of which about 30% is in C #, and the rest is in VB. Why did we decide to do this ?! Most likely because of the reluctance to work with VB and the lack of good vb-developers.
The deadlines that our leadership set for a complete transfer of projects, inexorably striving for something close to zero, colleagues from a contracting organization, also hurried to part with these projects and breathe a sigh of relief. Having postponed all non-urgent matters, having sat for a couple of meetings, we began to implement this idea.
')
Training
Any business begins with this. Even with a limit in time
and golden hands , we should not immediately rush into battle, we also did.
Due to specific differences of languages, it is necessary to prepare a project for conversion. We have identified several stages, after which it will be relatively painless to go through the conversion process.
Why is it relative ?! Yes, because after the conversion, in any case, there is a hint of unknown behavior of the program. This can come up immediately if your code is not covered by 100% of the tests. In our case, I'm not talking about tests at all, for there are none at all. Well, let's consider what steps we have identified:
1) Choosing a tool for conversionThis is a very important stage on such a difficult path, because the speed and quality of the cut will depend on the saw you choose. After numerous
Google questions and meetings, we settled on two products: Instant CSharp and SharpDevelop version 4.x (in version 5.x conversion is not supported), which is a very strange, in my opinion, solution.
SharpDevelop is free - this is undoubtedly a plus, but Instant CSharp has a free version, which does not differ in functionality from the paid version, only there is a limit on the number of lines in the converted project. The advantage of Instant CSharp is that it converts WinForms better. He also has a minus - this is the correct conversion of
only UTF files .
We decided to use Instant CSharp for small projects, and SharpDevelop for everything else.
2) Making a list of VB projects and forming a dependency diagramThis step is necessary in order to visually understand from which side to begin work. I will give an example of one of the dependency diagrams, which we got:

This diagram also shows the number of dependencies between projects. From the diagram, and according to the logic of things, it is clear that the order of transforming projects is from projects with fewer dependencies to projects with more dependencies.
3) Replacing ReDim with Array.Resize ()A small Re class was written to implement this replacement.
Public Class Re Public Shared Sub [Dim](Of T)(ByRef Source As T(), HighBound As Integer) // DimPreserve(Source, HighBound) // Array.Clear(Source, 0, HighBound + 1) End Sub Public Shared Sub DimPreserve(Of T)(ByRef Source As T(), HighBound As Integer) // Array.Resize(Source, HighBound + 1) End Sub End Class
Then, all calls to ReDim Preserve X (N) were replaced by Re.DimPreserve (X, N), and ReDim X (N) was replaced by Re.Dim (X, N).
4) Enable compilation option strictBy default, .NET Visual Basic or the Visual Basic compiler does not provide strong typing. To change this default behavior, you must in the project properties on the Compile tab set Option strict to the value on. After that, correct any errors that occurred.
The following points do not require clarification, as they are simple and straightforward. We just take
ReSharper in our hands and perform all the points described below with the speed of sound (and even faster).
5) For value type variables, replace Nothing with Default6) Replacing And with AndAlso and Or with OrElse7) Removing Imports Constructs that Reference Classes8) Replacing a method call like .Method with .Method ()9) Removing Exit Try Constructions10) Replace the comparison of string variables with Nothing or "" or with String.Empty to String.IsNullOrEmpty ()11) Replacing unnecessary ByRef directives with ByVal for method parameters12) Rename methods that have the name of their own class13) Correction of incorrect case-insensitive calls in VB codeConversion
And here we come to the final stage - the conversion stage. The tool is selected, the projects are prepared and are waiting for their fate.
Well - let's start!
The conversion process is no more complicated than the Hello World program, it only takes a little longer. I will cite a small tutorial, as is done in SharpDevelop and Instant CSharp.
SharpDevelop Conversion1. Copy the VB.NET project to a separate folder
2. Open a project in SharpDevelop (File-> Open-> Project / Solution)
3. Convert a project (Project-> Convert-> From VB.NET to C #)
4. Open the main solution in Visual Studio and delete the VB.NET project.
5. Delete the folder with the project VB.NET
6. Rename a folder and a file converted in C # project (remove the word Converted)
7. Copy the folder with the new project to the directory with the main solution
8. Add a copied project to the solution
9. Add links to the new project (C #) to all other projects that had links to the old one (VB.NET). To do this, you can use the version control system tool to view changes in the * .csproj and * .vbproj files
10. Try to compile the project. Fix any errors
Convert with Instant CSharpInstant CSharp is
not friendly with ANSI , so you must first convert the source file encoding to UTF8.
Convenient UTFCast Express Utility1. Open UTFCast Express
2. In the field Source directory specify the path to the project directory
3. In the Target directory field, specify the path to the directory to which the project files in the UTF8 encoding will be moved
4. Set the Copy option unconverted and start the conversion
And after these dances with a tambourine, you can convert the project.
1. Open Instant CSharp
2. In the VB project or solution field: specify the path to the project file * .vbproj
3. In the field C # target folder: specify the directory to which the project converted to C # will be saved
4. Convert a project (Project-> Convert-> From VB.NET to C #)
5. Open the main solution in Visual Studio and delete the VB.NET project.
6. Delete the folder with the project VB.NET
7. Copy the folder with the new project to the directory with the main solution
8. Add a copied project to the solution
9. Add links to the new project (C #) to all other projects that had links to the old one (VB.NET). To do this, you can use the version control system tool to view changes in the * .csproj and * .vbproj files
10. Try to compile the project. Fix any errors
Conclusion
As a result, we transferred both our projects completely to c #. Now, the refactoring of the code is underway, by the way, it will not be said in the advertisement, but we will not replace
ReSharper in this matter. During operation, sometimes errors occur due to conversion, but this is inevitable, alas.
I hope our little experience will be useful to you, dear habrazhiteli.
Links
Instant CSharpSharpDevelop