Table of Contents
Downloads
Background
This article is about converting code to VB.Net.
It will discus SharpDevelop
IDE and VB6 Upgrade Companion.
SharpDevelop
IDE and NRefactory cover many conversion between deferent
languages (C#, VB.Net, boo, Python, Ruby) and most other C# VB converter
use the library NRefactory.
VB6 Upgrade Companion
witch will convert VB6 codes to C# or VB.Net. This software is selected because
it is the only Microsoft partner to upgrade VB6
Before Conversion
- Make sure your code can be recompiled and run well before start converting.
- For C# and VB.Net 100% code conversion maybe get in many causes but
not always. In some cases many post conversion fixing is required.
- No convertors can grantee the 100% conversion.
- For other language pairs: Make sure the the target language support your
project type.
- Please note the VB6 converting is a hard work because of the programing language
philosophy is defer. please see VB6 section later is this article
SharpDevelop
Lots of C# <> VB code converter are available all most all of them are
using Refactory library form SharpDevelop IDE to make the conversion and them make
some useful post conversion fixing.
The power of NRefactory is parsing the code before the conversion; even if a
keyword could take more than one meaning it could be converted correctly because
of understanding the code line before the conversion. an other thing will make
the converting possible is that both languages are extremely similar.
About SharpDevelop
- It's an freeware Open Source Development Environment for .NET.
- It could be used to make many useful code conversion.
- SharpDevelop
IDE is the mother of NRefactory library which is the most uses converting
library
- The conversion that done
using SharpDevelop
IDE is much beater.
- It provide a wide range of code conversion C#,
VB.NET, Boo, Python, Ruby
About NRefactory:
- ICSharpCode.NRefactory is freely available as a part of SharpDevelop IDE.
- It
is parser library for C# and VB.
- It consists of a single Abstract Syntax Tree (AST) that can represent all constructs that are available in C# or VB (unlike System.CodeDom, which only represents constructs common to C# and VB).
- By using the C# parser and a VB output visitor (or vice versa), you can
do a
code converter.
- After parsing to AST, you can analyze and/or transform the AST and re-create source code from the (modified) AST,
then re-insert the comments we saved from the parser into the output
- For more info about NRefactory please see:
sharpdevelop.net and
NRefactory wiki.
- You may try samples\NRefactoryDemo in the SharpDevelop source code to take a look
how the AST parse source code.
About AST and Parsing:
The following example show a simple parsing proses.
SharpDevelop IDE and C# VB converters:
Application | Conversion Technique | What to convert | Conversion | Notes |
SharpDevelop IDE | Developer IDE freeware | Code & file | C#, VB.Net, boo, Python, Ruby | The Best |
Econ NetVert | Use NRefactory
| Code & file
| C# <> VB.Net
| |
On Line Converter/a> | Web page Online service | CCode only | C# <> VB.Net | |
Instant VB | Share ware Internal Converter | Code, file & folder | Special version for each conversion convert
between: C#, VB.Net, C++, Java/td>
| |
VB.Net to C# Converter | Use NRefactory | Code & file | VB.Net > C# | |
Convert .NET | Use NRefactory | Code only | C# <> VB.Net | |
C# to VB.NET Project Converter | Use NRefactory | Projects | C# > VB.Net | |
C# and VB Language Equivalents and Comparison
- Language Equivalents
- Comparison
of C# and VB
Using SharpDevelop as a code converter:
- Run SharpDevelop IDE
- From file menu select open - project/solution and open the project to be converted
- From View menu select projects
- In the project window select the project to be converted
- from project menu choose convert and then choose the target
language
- This will convert the whole project
- The conversion could be done by right clicking the project icon in
the project window then choose convert
SharpDevelop Points of Interest
How to covert C# code:
The code is converted using ICSharpCode.NRefactory.ParserFactory
class:
Converter code in VB:
Imports ICSharpCode.NRefactory
Imports ICSharpCode.NRefactory.PrettyPrinter
Imports System.IO
Public Class CS2VB
Public Overrides Function ConvertCode(ByVal sourceCode As String) As String
Dim specials As IList(Of ISpecial)
Dim result As Ast.CompilationUnit
Dim Parse_errors = ""
Using parser As IParser = ParserFactory.CreateParser( _
SupportedLanguage.CSharp, New StringReader(sourceCode))
parser.Parse()
specials = parser.Lexer.SpecialTracker.RetrieveSpecials()
result = parser.CompilationUnit
If parser.Errors.Count > 0 Then
Parse_errors = parser.Errors.ErrorOutput
End If
End Using
Dim outputVisitor As New PrettyPrinter.VBNetOutputVisitor
Dim astViewUnit = result
Using SpecialNodesInserter.Install(specials, outputVisitor)
astViewUnit.AcceptVisitor(outputVisitor, Nothing)
End Using
Dim outputCode = outputVisitor.Text
Return outputCode
End Function
End Class/pre>
Converter code in C#:
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.PrettyPrinter;
using System.IO;
public class CS2VB
{
public override string ConvertCode(string sourceCode)
{
IList specials;
Ast.CompilationUnit result;
object Parse_errors = "";
using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new
StringReader(sourceCode))) {
parser.Parse();
specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
result = parser.CompilationUnit;
if (parser.Errors.Count > 0) {
MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");
}
}
PrettyPrinter.VBNetOutputVisitor outputVisitor = new
PrettyPrinter.VBNetOutputVisitor();
object astViewUnit = result;
using (SpecialNodesInserter.Install(specials, outputVisitor)) {
astViewUnit.AcceptVisitor(outputVisitor, null);
}
object outputCode = outputVisitor.Text;
return outputCode;
}
}
Demo conversion:
Partial code convert is not supported
code should be in class and in method.
Demo1:
class Class1{void sub1(){
int x =0;
}}
The result in VB
Class Class1
Sub sub1()
Dim x As Integer = 0
End Sub
End Class
Demo 2:
namespace WindowsFormsApplication1{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.TextBox textbox1;
private System.Windows.Forms.TextBox textbox2;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.textbox1 = new System.Windows.Forms.TextBox();
this.textbox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textbox1.Location = new System.Drawing.Point(30, 30);
this.textbox1.Name = "TEXTBOX1";
this.textbox1.Size = new System.Drawing.Size(80, 20);
this.textbox1.TabIndex = 0;
this.textbox2.Location = new System.Drawing.Point(30, 60);
this.textbox2.Name = "textbox";
this.textbox2.Size = new System.Drawing.Size(80, 20);
this.textbox2.TabIndex = 1;
this.textbox2.TextChanged += new System.EventHandler(this.textbox_TextChanged);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.textbox2);
this.Controls.Add(this.textbox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
public Form1()
{
InitializeComponent();
}
private void textbox_TextChanged(object sender, EventArgs e)
{
string x = "";
string X = "";
}
}
}
The result in VB
Namespace WindowsFormsApplication1
Partial Class Form1
Private components As System.ComponentModel.IContainer = Nothing
Private textbox1 As System.Windows.Forms.TextBox
Private textbox2 As System.Windows.Forms.TextBox
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing AndAlso (components IsNot Nothing) Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.textbox1 = New System.Windows.Forms.TextBox()
Me.textbox2 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
Me.textbox1.Location = New System.Drawing.Point(30, 30)
Me.textbox1.Name = "TEXTBOX1"
Me.textbox1.Size = New System.Drawing.Size(80, 20)
Me.textbox1.TabIndex = 0
Me.textbox2.Location = New System.Drawing.Point(30, 60)
Me.textbox2.Name = "textbox"
Me.textbox2.Size = New System.Drawing.Size(80, 20)
Me.textbox2.TabIndex = 1
AddHandler Me.textbox2.TextChanged, New System.EventHandler(AddressOf Me.textbox_TextChanged)
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.textbox2)
Me.Controls.Add(Me.textbox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Public Sub New()
InitializeComponent()
End Sub
Private Sub textbox_TextChanged(sender As Object, e As EventArgs)
Dim x__1 As String = ""
Dim X__2 As String = ""
End Sub
End Class
End Namespace
Conversion Cases:
Case sensitive C# to VB:
The converter will automatically rename object if needed.
Event Handler C# to VB:
The event C# handler will be converted as follows:
C#:
this.textbox2.TextChanged += new System.EventHandler(this.textbox_TextChanged);
VB:
AddHandler Me.textbox2.TextChanged, New System.EventHandler(AddressOf Me.textbox_TextChanged)
VB6 Upgrade Companion VBUC:
If you are not familiar with upgrading VB6 then you will not under stand how
useful the software. It same too much of the upgrade time also it is in most
time required many post conversion works.
It is much better than the old upgrade engine that is included in versions
2003, 2005 and 2008 of VS
VB 6.0 upgrading advantages
- Supporting 64 bit
- Faster running
- More resources
- Improve the maintenance of an application
- Increase developer productivity
VB 6.0 upgrading disadvantages
- Compile on demand will be lost.
- The resulting application need dot net farmworker will the original one
is not
- If your application is a dynamic library; note that pre declared object
is not supported in C# or vb.Net.
- If your application is ActiveX control or ActiveX Document then there is
no acceptable converter for you; manual code rewriting is needed.
- The converter we save your time for converting but post conversion work
is much needed.
What VBUC can upgrade:
- Modules, Classes, Forms & MDIForms
- User Controls that used in the same project on in the same group
- Resource File
What VBUC can't upgrade:
- Property Page
- Designer files (*.Dsr)
- User Controls that used in web browser or ActiveX container
- ActiveX Document
Before Using VBUC:
The VBUC is feather software with many option and we should study before
start converting:
- It is better to install VB6 but it is not a must
- Ensure that your application could be recompiled and run correctly.
- Ensure that all your referred library are available and work well in the
current operating system.
- It is more simple to convert to VB.Net than C#.
- For ActiveX choose Com Visible
- Form options Use helper classes whenever it available
- For making you application more like .net designed use More dot net
option
- If the above option fail use the More Automation options for less error
in conversion.
- After converting complete open your project in VS and disable all
warning and switch option explicit off and option strict off.
- If error is still appears try to fix them manually.
- Once you are able to compile your project try to follow warning
- Switch option explicit on and option strict on and follow any errors
appears
VBUC Trial version limitation:
It is work for project with less than 10000 line code
Other VB6 Converters:
Other Converters:
VB6 to .NET
ASP to .NET
Java to .NET
PHP to .NET
C++ to .NET
ColdFusion to .NET
History