Converting VB to C#

Follow John Browne on a brief history of Visual Basic and learn how to convert VB code to C# easily.

BASIC as a programming language dates back to 1964 when Dartmouth professors John Kemeny and Thomas Kurtz decided to create a very, well, “basic” programming language to teach students how to program. At the time, the existing languages were either extremely low-level or, like COBOL, too ugly to look at.

In 2014 Dartmouth celebrated the 50th anniversary of the creation of BASIC:

  • If BASIC were a human, its kids would be middle-aged.
  • If BASIC were a US President, it would be in Lyndon Johnson’s grave.
  • If BASIC were a pop song, it would be “I want to hold your hand.”

BASIC is OLD

But it was popular. All those cute little home computers, like the Apple II and the Commodore 64—and even the original IBM PC—came with BASIC. And lo and behold, the masses took to BASIC like my Border Collie takes to leftovers. And after all those minions learned to program by writing silly games, many turned their attention to serious business problems and wrote more code.

Then in the 80s along came Microsoft Windows with a GUI and mouse events and bitmapped graphics. But writing Windows code was really really hard. It was like assembly language but more mean-spirited. Everything was a pointer, you had a message pump (whatever the heck that was), you had to manage your memory, and the documentation read like Sanskrit. So when Visual Basic arrived on the scene in 1991, all those BASIC developers jumped on it like my Border Collie on medium rare prime rib.

No more line numbers, no more PRINT statements to debug, easy form design... it was heaven. The boxes flew off the shelves. A huge ecosystem of libraries and tools sprung up. People who had never written a program turned in to software developers overnight.

And we all know what happened next. With the release of .NET, Microsoft turned the beloved VB into VB.NET, which looked alarmingly like a “real” programming language—in fact, it suspiciously resembled the C# language that had been created for the sole purpose of writing apps for .NET.

Goodbye Visual Basic. Hello VB.NET.

The thing is, the two languages (VB.NET and C#) are NOT interchangeable. They both have access to the entire .NET framework, and they both use the same compiler and IL, but there are syntactic differences that persist. Enough people think VB.NET is still more approachable and “human readable” than C# to keep it alive. But the times, they are a-changin’.

Microsoft has laid out the roadmap for all their .NET languages, and C# got in the driver’s seat and VB.NET is in the back seat. C# will forever be a first-class language and VB will be the runt of the litter. Improvements will happen first in C# and later—if at all—incorporated into VB. As Microsoft turns its focus from the .NET framework to .NET Core, VB will get implemented after C# support is rolled out. And so on.

VB.NET or C#? You choose.

Which brings me to this cool tool from my buds on the Telerik team. You can paste your VB.NET code in and boom! It’s converted to C#. (Ok, you can go the other direction, too, but really who would do that?) I think this could be pretty helpful for folks who are used to VB and want to see how different the same function or sub procedure would look in C# (hint: it won’t be a sub…end sub anymore).

A quick check on my part shows this little snippet here (C#):

int i = 0;
	fgOrders.RowsCount = modConnection.rs.RecordCount + 1;
		if (fgOrders.RowsCount == 1)
		{
			fgOrders.FixedRows = 0;
		}
		else
		{
			fgOrders.FixedRows = 1;
		}
		i = 1;
		while (!modConnection.rs.EOF)
		{
			int tempForEndVar = modConnection.rs.FieldsMetadata.Count - 1;
			for (int j = 0; j <= tempForEndVar; j++)
			{
				if (modConnection.rs.GetField(j) != null)
				{
					fgOrders[i, j].Value = Convert.ToString(modConnection.rs[j]);
				}
			}
			modConnection.rs.MoveNext();
			i++;
		}

When pasted into their converter, yields this (VB.NET):

Dim i As Integer = 0
        fgOrders.RowsCount = modConnection.rs.RecordCount + 1

        If fgOrders.RowsCount = 1 Then
            fgOrders.FixedRows = 0
        Else
            fgOrders.FixedRows = 1
        End If

        i = 1

        While Not modConnection.rs.EOF
            Dim tempForEndVar As Integer = modConnection.rs.FieldsMetadata.Count - 1

            For j As Integer = 0 To tempForEndVar

                If modConnection.rs.GetField(j) IsNot Nothing Then
                    fgOrders(i, j).Value = Convert.ToString(modConnection.rs(j))
                End If
            Next

            modConnection.rs.MoveNext()
            i += 1
        End While

Other than converting (blessed and deeply loved) tabs into (hated and horrid) spaces, this conversion seems pretty solid. Admittedly this isn’t a particularly difficult example to test with.

What about VB6?

It does, however, leave open the issue of dealing with the mother of VB.NET: VB6. Or, as some people call it, Real Visual Basic. VB6 is NOT VB.NET —different syntax, different runtime library, and different forms package. VB6 is out of support—has been for years now—and it’s getting harder and harder to find people who can or will work on VB6 applications. And believe it or not, there are still millions of lines of VB6 code running in the real world, in many cases as mission-critical applications inside the enterprise.

Fortunately, when VB.NET was released, Microsoft hired Mobilize.Net to build a migration tool to convert VB6 code to .NET code. That tool—which used to be included with Visual Studio but alas, isn’t anymore—has, over the subsequent years, been improved until it is the most widely-used conversion tool for VB6 to .NET. It quickly and easily converts VB6 code, forms, and runtime to C# or VB.NET using the .NET Framework and Windows Forms. It will even let VB6 developers convert their app into a modern Angular-based web application with ASP.NET Core on the back end, using a follow-on tool called WebMAP from the same company. And you can try it out on your own code for free.

If you’re still in the Visual Basic world—whether VB6 or VB.NET—consider moving to C#. Among other reasons, new stuff like Blazor—which Telerik has a cool new UI toolset for—are C#-based, not VB.NET. Frankly, if you’ve already learned VB.NET, C# will be an easy transition. And if you’re still on VB6, you might as well jump to C# and say adios to Visual Basic.


JDB-Headshot
About the Author

John Browne

John Browne has been working on software since computers were steam-powered and PRINT statements were debuggers. John spent 11 years at Microsoft during the transition from DOS to Windows has been variously a college professor, a corpse hauler, an auto mechanic, a sailor of boats, a racer of cars, and a National Lecturer for the ACM. Since 2011 John has been Technical Product Manager for Mobilize.Net, where he spends his days working to convince customers that C# is more fun than VB6.

Related Posts

Comments

Comments are disabled in preview mode.