Thinking in .NET
You might have heard that all programs generated in .NET execute the Common Language Runtime (CLR). Prior to .NET, all programs would run against their own runtime library. C++ used MSVCRTxx.DLLs, and VB used the MSVBVM60.DLL. Now, all .NET orograms, regardless of which language they were written in, use the .NET Framework's CLR as it source library to execute against.
When going from VB to .NET, probably the biggest change you have to make is thinking in an object-oriented manner. You can't code in VB.NET or C# without thinking this way. You have to think about things like constructors/destructors, inheritance, overloading, polymorphism, and so on.
If you find yourself struggling with some of the differences between how things were done in VB6 and how it's suppose to be in VB.NET, the IDE has a tool called "Upgrade Visual Basic 6 Code" found under the Tools menu. What you do is enter in a VB6 code snippet and it will do its best to translate it to VB.NET syntax. You can also select COM objects (see Figure 1) and it will create the necessary entries in your applications reference section (see Figure 2).
Unfortunately, this tool cannot be found in Visual Studio .NET 2002. Microsoft included it in Visual Studio .NET 2003, and it only works with VB6 code to VB.NET. Remember, not all the VB code you enter in will translate into VB.NET code. The following example is a simple procedure to a lot of VBers used to center a form when it loads:
Me.Move ((Screen.Width - Me.Width) / 2), ((Screen.Height - Me.Height) / 2)
This is the translation from the Upgrade Visual Basic 6 Code tool:
'UPGRADE_WARNING: Couldn't resolve default property of object Me.Height. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"' 'UPGRADE_WARNING: Couldn't resolve default property of object Me.Width. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"' 'UPGRADE_WARNING: Couldn't resolve default property of object Me.Move. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"' Me.Move((VB6.PixelsToTwipsX _ (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) _ - Me.Width) / 2, (VB6.PixelsToTwipsY _ (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) _ - Me.Height) / 2)
The correct VB.NET syntax is the following:
Me.SetBounds((System.Windows.Forms.Screen.GetBounds(Me).Width / 2) - (Me.Width / 2), _ (System.Windows.Forms.Screen.GetBounds(Me).Height / 2) - (Me.Height / 2), _ Me.Width, Me.Height, System.Windows.Forms.BoundsSpecified.Location)
And, in case you wanted to know how it would look in C#:
this.SetBounds((Screen.GetBounds(this).Width/2) - (this.Width/2), (Screen.GetBounds(this).Height/2) - (this.Height/2), this.Width, this.Height, BoundsSpecified.Location);
If you're trying to go from VB6 to C#, you're going to have to find that information elsewhere. Luckily, a number of savvy Web sites offer help in this area. Some offer free code translator online and some offer version that can be used offline. These are just a few:
- C# to VB.NET Translator: An online system that converts your C# code to VB.NET
- KamalPatel.NET: A Microsoft MVP who has an online and offline C# to VB.NET converter
- Ellkay.Com: A software development group in NJ (that Kamal Patel works at) that has an online VB.NET to C# converter
If you perform an Internet search using engines like Google or Yahoo, just enter "vb to c#"and you'll be amazed on how many tutorials and conversion tools there are to assist you.