Monday, July 18, 2005

VB to VB.NET and then to C# conversion

There is no 100% automatic conversion tool to let you convert the old VB code to VB.NET/C# with no effort. The tools can help you to convert the syntactic parts and the basic parts, while leaving the more sophisticate part to you and list them in a report as your ToDo list.

To convert from VB to VB.NET, Microsoft VS.NET has a conversion tool for you. When you open a project, choose convert, and choose Visual Basic.NET Upgrade Wizard, you will be asked to pick a VB project file (.VBP) to convert to a "unfinished" VB.Net project.

From my experience, the conversion are pretty good, except for the Screen drawing part. In VB, you can draw a line, a circle, or paint a circle on a PictureBox pic (or a form Form1) by doing things like below:

Me.DrawWidth = 3
pic.FillColor = vbBlue
pic.FillStyle = vbDownwardDiagonal
pic.Circle (ScaleWidth / 2, ScaleHeight / 2), _
pic.ScaleWidth * 0.45, vbRed, , , _
pic.ScaleHeight / pic.ScaleWidth

But in VB.NET, you need 3-4 objects to accomplish this. You will need to have a Graphics object gr as the place to draw; a Pen object ellipse_pen for drawing lines; and a Brush object ellipse_brush for painting colors. If you want to write some text on the form/picturebox, you will have to have a Font object too.

Dim gr As Graphics = Me.CreateGraphics()
Dim ellipse_brush As New HatchBrush( _
HatchStyle.BackwardDiagonal, _
Color.Blue, Me.BackColor)
gr.FillEllipse(ellipse_brush, 0, 0, _
Me.ClientSize.Width, Me.ClientSize.Height)
Dim ellipse_pen As New Pen(Color.Red, 5)
gr.DrawEllipse(ellipse_pen, 0, 0, _
Me.ClientSize.Width, Me.ClientSize.Height)

Not only that, in VB, you can draw under Form_Load event, and the system will take care of re-paint for you. But in VB.NET, all the drawing/painting part has to be in Paint Event Handler. Otherwise, the run time environment will not repaint for you like in VB. This can be a big confusion for most of the new VB.NET Developers.

Once you have a VB.NET program up and running, you are already in .NET domain. If you need to convert it to C#, there is another web based tool which can help:

http://www.carlosag.net/Tools/CodeTranslator/Default.aspx

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home