Site Archive (Complete)
Windows/.NET
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
February 21, 2006
Migrating VB.NET 2005 to C# 2.0, pt. 2

Mark M. Baker
What are the issues I will face in migrating VB.NET 2005 code to C# 2.0, and vice-versa?
Migrating VB.NET 2005 to C# 2.0, pt. 2

Part 2

While it's true that VB.NET 2005 and C# 2.0 are remarkably close in their feature set to the point that the differences in their expressiveness are fairly insignificant, they do differ in obvious and subtle ways with similar constructs. This is more understandable if you think back for a moment on the evolution of VB.NET and C# and how this has impacted the features in each language.

VB.NET 2005 is the latest edition of the venerable Visual Basic language. There are those on the internet that quibble over the improvements to VB.NET and whether it's still really Visual Basic in spirit, but for me it's still very much a Basic dialect in both style and usage. While retaining Visual Basic syntax from earlier editions, additional syntax and keywords were added to the language to bring VB.NET up to par with modern, class-oriented development languages such as Java, Python and C#. An example is the addition of the Try/Catch/Finally statement which finally gives VB developers the same exception-handling power that C++, C# and Java developers have had. No longer does the VB developer need to use the now antiquated OnErrorGoto or OnErrorResumeNext statements to manage errors.

The catch, of course, is that VB.NET (unlike C#) didn't start from a clean design slate&#mdash;for the most part it brought forward many Visual Basic "isms" into VB.NET that can complicate the process of migrating VB.NET code to a language such as C#. Some of these were for compatibility reasons while others were to retain the flexible and forgiving nature of programming in a Basic dialect. We'll take a look at some of these in a moment.

C# 2.0 is the latest edition of the Microsoft-designed C# language created by design guru Anders Hejlsberg. Anders has a long history in the PC environment designing languages that were ahead of their time. At Borland, Anders designed the Turbo Pascal language that was the arguably one of the most important advances in rapid development technology in the 1980's. Later he went on to design the Delphi language which borrowed ideas from Turbo Pascal and other languages to move the state of the art forward even more. In the mid 1990's, Anders went to work at Microsoft on the Visual J++ effort. When the .NET framework was being conceived, he began the design of a new language that would be optimized and focused on this new platform. This language went through a series of internal and beta names such as "COOL" finally ending up with the moniker C#. As could be expected from a language that used "C" in its name, C# is accepted as the latest evolutionary step in the 'C' language dialect. C# builds on the experiences of the earlier C++, the Java language (which proved that features such as garbage collection, first-class exception handling, single-file class definitions, etc. were now possible and feasible), and other languages such as Python.

The distinct advantage C# had at the beginning is that it started (for the most part) with a clean slate. Although influenced by predecessors such as C++ and Java, the designers of C# could elect to drop support for constructs that were perceived as dangerous (such as pointers), unnecessary (such as macros) or archaic (such as separating class definition from class implementation). There was never an expectation that 'C', C++ or even Java code would be re-compiled as C# code without a potentially substantial upgrade process that would be for the most part manual in nature.

So let's look at some examples of constructs in VB.NET 2005 that inherit from the Visual Basic legacy.

The first is the support in VB.NET for strings and their management. In Visual Basic 6.0, there was a String type which was a first-class string data type. A string could be assigned, copied, concatenated, returned as a value, passed to a function as a reference, and so on. However, since Visual Basic 6.0 had a weak notion of classes, the String type was considered a primitive data type similar to an Integer. In order to operate on a String, such as calculating its length, you had to use functions from the Visual Basic string "library." So to calculate the length of a VB6 string you would do the following:

	Dim s As String
	s = "Hello World"
	Dim size As Integer
	size = Len(s)
The code fragment creates a String variable s assigns the text "Hello World" to it and then calls the function Len to calculate its length.

In VB.NET, you could write the exact same code and it would produce the exact same results. The VB6 String type becomes the .NET System.String class, which in VB.NET is just referred to as String since the System namespace is typically imported by default. VB.NET improved on VB6 a bit in this area by allowing initialization-at-declaration similar to other modern languages, so this code could be tweaked to the following:

	Dim s As String = "Hello World"
	Dim size As Integer = Len(s)
This is definitely easier to read and understand than the VB6 version. However, we can improve on this a bit more by recognizing that in .NET the System.String class now has properties and methods of its own that don't require using library functions:

	Dim s As String = "Hello World"
	Dim size As Integer = s.Length

Again, this code produces the same result as the VB6-style version. You may be wondering at this point why both approaches are supported. As I described earlier, it's due to the heritage of VB.NET coming from Visual Basic and the desire on the part of the VB.NET team to allow for upgrading of VB6 applications to VB.NET (which the C# team didn't have to worry about). So VB.NET has a great many of these historic and now obsolete constructs that duplicate features provided by the .NET framework. In essence, VB.NET brought forward features that were in its predecessor's runtime and merged them with its new .NET runtime. This allows VB6 applications to be more easily upgraded, since this type of syntax can be left as-is and simply recompiled into .NET.

Next time, we'll continue with a look at how this code would be translated to C# and the issues that occur due to the VB.NET legacy constructs.

Whether you are moving to .NET from Win32 or have been using .NET since 1.0, I encourage you to listen to .NET Cast, an Internet radio show that I host and produce for CMP Media. Hear from the people behind .NET and key experts in the industry working with it. For those of you with RSS readers, you can find the feed at http://syndication.sdmediagroup.com/feeds/public/cmp_podcast_dotnet.xml.


Mark M. Baker is the Chief of Research & Development at BNA Software located in Washington, D.C.
Do you have a Windows development question? Send it to dotnetcast@sunburstsoftware.com.


TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     



    Related Sites: DotNetJunkies, SD Expo, SqlJunkies