Review: Instant C# and Instant VB

0
1460
Tangible Software Solutions

Visual Basic and C# represent two sides of the Microsoft Visual Studio coin. Though they began with very different purposes—Visual Basic as a simpler interface to powerful computers, C# as a powerful replacement for simpler assembly language systems—they now provide nearly equal access to the Microsoft Windows platform and beyond.

Arguments persist about which language is better, but thanks to Tangible Software Solutions’ Instant C# and Instant VB conversion tools, the arguments are mostly irrelevant. Using these tools, you can convert language snippets, ASP.NET pages, or entire projects between the two languages. The resulting code is, in many cases, ready for you to compile and use at once, although you will probably want to pass through the converted source and make adjustments based on each language’s strengths and weaknesses.

Note: Tangible offers Instant C# and Instant VB as two distinct software products. Except for the order of the languages involved, they are basically the same product. I tend to convert content from Visual Basic to C#, and not as much in the other direction, so this review focuses on the Instant C# tool.

The main user interface is nothing to get excited about: one button used to browse for the source project, another for the destination folder, and one that says “Convert.” Tangible tried to gussie things up with lists that display conversion statistics from prior uses of each app. But why worry about the interface when the goal is to spend as little time within the program as possible? The real magic appears in the new .NET project that the tools generate, and you will typically use Visual Studio or your favorite code editor for that.

Despite the muted user interface, that Convert button does wonders. Conversion from input to output is fast. Some of the smaller projects I’ve converted took less than a second. My largest project, a 215,000-line desktop behemoth with 240 forms took just over four minutes. In this mobile-enabled instant society, four minutes seems like an eternity. But here’s what happened during those four minutes: Instant C# wrote a nearly functional 215,000-line C# application. That’s wild!

The resulting code is as decent as the original project: Garbage In, Garbage Out is an issue here, but it’s your issue, not Tangible’s. Consider this Visual Basic code as food for Instant C#.

    Public Class Form1
        Private Sub Button1_Click(sender As System.Object,
                e As System.EventArgs) Handles Button1.Click
            MsgBox(GenerateMessage("Barack", #8/4/1961#))
        End Sub
        Private Function GenerateMessage(ByVal firstName As String,
                ByVal birthDate As Date) As String
            ' ----- Format a nice greeting.
            Dim age As Integer
            age = Today.Year - birthDate.Year
            If (Today.Month < birthDate.Month) Or
                    (Today.Month = birthDate.Month And
                    Today.Day < birthDate.Day) Then
                age -= 1
            End If
            Return "Hello " & firstName & ", you are " &
                age & " years old."
        End Function
    End Class

The conversion generates equivalent, good-looking C# code (some lines rewrapped to fit this article’s display area).

    public partial class Form1
    {
        internal Form1()
        {
            InitializeComponent();
        }
        private void Button1_Click(object sender,
            System.EventArgs e)
        {
            MessageBox.Show(GenerateMessage("Barack",
            DateTime.Parse("8/4/1961")));
        }
        private string GenerateMessage(string firstName,
            DateTime birthDate)
        {
            // ----- Format a nice greeting.
            int age = 0;
            age = DateTime.Today.Year - birthDate.Year;
            if ((DateTime.Today.Month < birthDate.Month) |
                (DateTime.Today.Month == birthDate.Month &
                DateTime.Today.Day < birthDate.Day))
            {
                age -= 1;
            }
            return "Hello " + firstName + ", you are " +
                age.ToString() + " years old.";
        }
    }

It looks like C# to me. Granted, there are some interesting changes. All identifiers now include initializers, as with the “age” variable in the sample code. That’s because most initialization is optional in VB, but required in C#, and Instant C# knows this. It also favors explicit conversions, including the addition of the “ToString” method on the “age” variable when concatenating the result, again a reflection of differences between the languages. And don’t worry about the missing “Handles” clause from Button1’s Click event handler. Its match has been moved to the form’s code-behind file, exactly where Visual Studio would place it in a new C# project.

Despite the converters’ language knowledge, not everything comes across in a ready-to-compile state. Each language includes “issues” that don’t translate well to the other. For instance, there is no “On Error Resume Next” construct in C#—no unstructured error handling at all—so that statement shows up in the C# result unmodified other than the addition of an accompanying warning comment. Likewise, C#’s “unsafe” keyword doesn’t do well in the move to VB. Any code in an unsafe block is simply commented out in the new Visual Basic project.

Some aspects of the converted code may introduce subtle differences in how your code manipulates data. For example, Visual Basic’s conversion methods (CInt, CDate, and so on) forgive a multitude of data sins that aren’t even considered in C#’s System.Convert equivalents. Tangible’s tools convert these language components without any warnings on the assumption that you are a good programmer and know better than to let questionable data touch your language’s magic keywords. Still, any conversion, even a manual conversion, is going to be imperfect, and with Instant C# and Instant VB doing at least ninety percent of the work, who’s complaining?

For that final ten percent, you might want to reference the pages of the C#-Visual Basic Bilingual Dictionary, written by your humble reviewer. It provides C# equivalents for every Visual Basic keyword, and vice versa, along with caveats and warnings that you should think through after Tangible’s super software has done its work.

Each Instant… tool includes an options panel that lets you adjust some of the conversion specifics, both for things that impact the output logic and for aesthetic differences. (The options panel from Instant VB appears here.) Each program also converts project files, configuration files, and other supporting elements of your project. It does a good job at leaving things it doesn’t know how to convert untouched. For example, a typical .vbproj file contains configuration items that aren’t useful in the equivalent .csproj file. These XML elements simply come across into the new project file, just in case they are needed later.

Although I started my programming career decades ago in the C world, I’ve focused on Visual Basic for much of the last twenty years. But I find myself moving back to my C-language roots, and Tangible Software Solutions’ Instant C# is a regular part of that migration. I use the tool weekly, not only to convert some legacy code from nearly fifteen years ago, but also to save time when grabbing code snippets off of MSDN and StackOverflow.

Tangible also offers converters that move code between C++ and Java, and between those languages and the C# and VB platforms. If the C# and VB versions are an accurate gauge, then the Java and C++ variations will be fantastic. To find out more about Tangible Software’s conversion tools and other products, visit their web site, www.tangiblesoftwaresolutions.com.

LEAVE A REPLY

Please enter your comment!
Please enter your name here