Code Converter: VB6 VB C# PHP Java

Code Converter: VB6 VB C# PHP Java

Code Converter article focuses on upgrading VB6 to VB.Net and convert C# <=> VB.Net, and listing many other helpful converters, including PHP, Java & others.

Table of consents of Code Converter:

Downloads

Downloads for Code Converter

Downloads of C# & VB.Net Converters Using NRefactory v4:

Downloads of C# & VB.Net Converters Without Using NRefactory v4:

Downloads of VB6 Upgrader:

Downloads of C# & VB Helper Library & related code:

Downloads of PHP to .NET:

Downloads of Java & .NET:

Downloads of C++ to .NET:

Downloads of ColdFusion to .NET:

Before Conversion:

  • Make sure your code can be re-compiled and run well before start converting.
  • For C# and VB.Net 100% code conversion maybe get most times but not always. Most times, we need fixing after conversion.
  • No converters can guarantee 100% conversion.
  • For other language pairs: Make sure the target language supports your project type.
  • Please note the VB6 converting is a hard job because the programing language philosophy is not the same. Please see the VB6 section later in this article

C# & VB.Net:

SharpDevelop:

Lots of C# <> VB code converter are available all most all of them are using NRefectory the library from SharpDevelop IDE to make the conversion and then make some useful post-conversion fixing.

The power of NRefactory v4 is parsing the code before the conversion; even if a keyword could take over one meaning, they could convert correctly it because of understanding the code line before the conversion. Another thing that will make the converting is possible in most cases.

About SharpDevelop

  • It’s a freeware Open Source Development Environment for .NET
  • It could make much useful code conversion.
  • SharpDevelop IDE is the mother of NRefactory the version 4 of the library which is the most uses converting library
  • The conversion that is done using SharpDevelop IDE is much better.
  • It provides a wide range of code conversion C#, VB.NET, Boo, Python, Ruby

About NRefactory v4:

  • ICSharpCode.NRefactory is freely available as a part of SharpDevelop IDE version 4.
  • It is a parser library for C# and VB.
  • It comprises 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 (changed) AST, then re-insert the comments we saved from the parser into the output
  • For more info about NRefactory v4 please see sharpdevelop.net and NRefactory wiki.
  • You may try samples\NRefactoryDemo in the SharpDevelop source code to take a look at how the AST parses source code.

About AST and Parsing:

The following example shows a simple parsing process.

SharpDevelop IDE and C# VB Code Converter:

Application Technique What to
convert
conversion notes
SharpDevelop IDE Developer IDE freeware Code & file C#, VB.Net,
boo, Python, Ruby
VB.Net to C# Converter Use NRefactory v4 Code & file VB.Net > C#
Convert .NET Use NRefactory v4 Code only C# <> VB.Net
C# to VB.NET Project Converter Use NRefactory v4 Projects C# > VB.Net
Instant VB Shareware
Internal Converter
Code, file
& folder
Special version for
each conversion
convert between:
C#, VB.Net, C++,
Java
Very Flexible

What is a C# code that has no equivalent in VB?

  • C# Fixed-size buffers and fixed statement has no equivalent in VB:
    private fixed char name[30]
  • C# fixed block has no equivalent in VB:
    Enter the fixed keyword. When used for a block of statements, it tells the CLR that the object in question cannot be moved, and thus, it ends up pinning the object. Thus, when pointers are used in C#, the fixed keyword is used pretty often to prevent invalid pointers at runtime.
  • C# Generic indexers and properties have no equivalent in VB:
    public T this[string key] { 
    	get { /* Return generic type T. */ } 
    }
    public T GetItem<T>(string key) { 
    	/* Return generic type T. */ 
    }
  • C# Generic operators have no equivalent in VB:
    public static T operator +<T>(T a, T b)	{
    // Do something with a and b that makes sense for operator + here
    }
  • C# #undef has no equivalent in VB.
  • C# volatile/checked/unchecked/stackalloc has no equivalent in VB:
    Using the unchecked statement with constant expressions, Overflow is unchecked at compile time or runtime and the return value be differs
  • C# unsafe block has no equivalent in VB:
    unsafe static void FastCopy ( byte[] src, byte[] dst, int count ){
    // unsafe context: can use pointers here
    }
  • C# extern alias has no equivalent in VB:
    It can sometimes be necessary to reference two versions of assemblies that have the same fully qualified type names, for example, when you need to use two or more versions of an assembly in the same application. By using an external assembly alias, the namespaces from each assembly can be wrapped inside root-level namespaces named by the alias, allowing them to be used in the same file.
    To reference two assemblies with the same fully qualified type names, we must specify an alias on the command line:

    /r:GridV1=grid.dll
    /r:GridV2=grid20.dll

    This creates the external aliases GridV1 and GridV2. To use these aliases from within a program, reference them using the extern keyword. For example:

    C#
    extern alias GridV1; 
    extern alias GridV2;
  • C# overloading ++ or — operator in has no equivalent in VB: ++ & — can convert easily but overloading is not.
  • C# ?? operator has no equivalent in VB

What is a VB code that has no equivalent is C#?

  • VB parametrized properties have no equivalent in C#
    Public Property MyProperty(ByVal A As String) As String
        Get
            Return 2 * A
        End Get
        Set(ByVal value As String)
            A = value / 2
        End Set
    End Property
  • VB Numeric labels, On Error, Resume Next, Continue, and Err object has no equivalent in C#
    VB
    Sub Test()
        On Error Resume Next
        Err.Raise(1000)
        On Error GoTo 0
        Err.Raise(1001)
        On Error GoTo -1
        Err.Raise(1002)
        On Error GoTo EH
        Err.Raise(1003)
        Exit Sub
    EH:
        MsgBox(Err.Description)
        Resume Next
    End Sub
  • VB IIf is some who defer than if in C#.
    A = IIf(E, B, A)
  • VB ReDim Preserve has no built-in equivalent in C#
    ReDim Preserve A(9)
  • VB Select Case: switch is not as flexible as Select Case, and we can not convert many VB Select Case blocks cannot to switch
    VB
    Select Case A
        Case 1 To 10
            '
        Case Is < 2 * A
            '
        Case Is > 30
            '
    End Select
  • VB Procedures local static variables have no built-in equivalent in C# but class-level private variable may use.
    Sub Test()
        Static InUse As Boolean = False
        If InUse Then Exit Sub
        'Run Once code
        InUse = True
        '
    End Sub
  • VB With has no equivalent in C#.
    With Me
        .Width = 10
        .Top = 0
    End With
  • It does not support VB Import Shared Class member in C#
    Imports WindowsApplication1.Form1
  • VB Exit try
  • VB Exit statements that not matching the immediately enclosing block.
  • VB Member names can be the same as their enclosing type but in C# can’t.
  • VB #Const can be set to any value but in C# accept constants only
    #Const C = 2 * A
  • VB MyClass has no equivalent in C# (sometimes using this is OK)
  • In C#, an object cannot reference itself in its class-level declarations: You cannot use this in class-level declarations in C#.
  • In C#, an object cannot reference its base class in its class-level declarations: You cannot use base in class-level declarations in C#.
  • Numeric labels: C# does not allow Numeric labels, You must change the label to be a standard C# identifier.
  • VB &O has no equivalent in C#
  • VB When has no equivalent in C#
  • VB has some operator that is not present in C# and we can’t be overload it in C# in any way such as(integer division (VB \), Like, exponentiation (VB ^))
  • VB Method with the same name as the Class name: C# used class name for constructor in C# and can’t use for naming any other method or property in C#.
  • VB integer casts convert ‘True’ to -1, but System. Convert methods convert ‘True’ to 1.

C# and VB Language Equivalents and Comparison:

  1. Language Equivalents
  2. Comparison of C# and VB
  3. Complete Comparison for VB.NET & C#

Using SharpDevelop as a code converter:

  1. Run SharpDevelop IDE
  2. From the file, menu select open – project/solution and open the project to be converted
  3. From the View menu select projects
  4. In the project, the window selects the project to be converted
  5. from the project, menu choose the convert and then choose the target language
  6. This will convert the entire project
  7. We can do the conversion could by right-clicking the project icon in the project window then choose convert

SharpDevelop Points of Interest

How to convert C# code:

I convert the code 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()
                'this allows retrieving comments, preprocessor directives, etc. 
                '(stuff that isn't part of the syntax)
                specials = parser.Lexer.SpecialTracker.RetrieveSpecials()
                'this retrieves the root node of the result AS
                result = parser.CompilationUnit
                If parser.Errors.Count > 0 Then
                    Parse_errors = parser.Errors.ErrorOutput
                End If
            End Using

            'Now you can analyze and/or transform the AST.
            'Should you want to re-create source code from the (modified) AST, 
            'use an output visitor: 
            're-insert the comments we saved from the parser into the output
            
            Dim outputVisitor As New PrettyPrinter.VBNetOutputVisitor
            Dim astViewUnit = result
            Using SpecialNodesInserter.Install(specials, outputVisitor)
                astViewUnit.AcceptVisitor(outputVisitor, Nothing)
            End Using
            Dim outputCode = outputVisitor.Text
            'By using the C# parser and a VB output visitor (or vice versa), 
            'you can build a code converter. Of course, 
            'the real Code Converter in SharpDevelop also transforms the AST to fix cases 
            'where C# and VB semantics differ.

            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)
    {
        //var names and format is as in http://wiki.sharpdevelop.net/NRefactory.ashx

        //ICSharpCode.NRefactory is a 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).
        //Please try samples\NRefactoryDemo in the SharpDevelop source code 
        //to take a look at the AST
        //To parse source code, use: 
        IList specials;
        Ast.CompilationUnit result;
        object Parse_errors = "";
        using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new 
                StringReader(sourceCode))) {
            parser.Parse();
            // this allows retrieving comments, preprocessor directives, etc. 
            //(stuff that isn't part of the syntax)
            specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
            // this retrieves the root node of the result AST
            result = parser.CompilationUnit;
            if (parser.Errors.Count > 0) {
                MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");
            }
        }
        //Now you can analyze and/or transform the AST.
        //Should you want to re-create source code from the (modified) AST, 
        //use an output visitor: 
        //re-insert the comments we saved from the parser into the output
        PrettyPrinter.VBNetOutputVisitor outputVisitor = new 
                PrettyPrinter.VBNetOutputVisitor();
        object astViewUnit = result;
        using (SpecialNodesInserter.Install(specials, outputVisitor)) {
            astViewUnit.AcceptVisitor(outputVisitor, null);
        }
        object outputCode = outputVisitor.Text;
        //By using the C# parser and a VB output visitor (or vice versa), 
        //you can build a code converter. Of course, 
        //the real Code Converter in SharpDevelop also transforms the AST 
        //to fix cases 
        //where C# and VB semantics differ.

        return outputCode;

    }
}

Demo conversion:

We can not make a partial code conversion. the code should be in class and the method.

Demo1:

class Class1{void sub1(){
    //your code starts
    int x =0;
    //your code ends
}}

The result in VB

Class Class1
    Sub sub1()
        'your code start
        Dim x As Integer = 0
        'your code end
    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();
            // 
            // textbox1
            // 
            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;
            // 
            // textbox2
            // 
            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);
            // 
            // Form1
            // 
            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()
			' 
			' textbox1
			' 
			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
			' 
			' textbox2
			' 
			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)
			' 
			' Form1
			' 
			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

Code Converter Cases:

Case sensitive C# to VB:

The converter will automatically rename the object if needed.

Event Handler C# to VB:

It will convert event C# handler:

C#:

this.textbox2.TextChanged += new System.EventHandler(this.textbox_TextChanged);

VB:

AddHandler Me.textbox2.TextChanged, New System.EventHandler(AddressOf Me.textbox_TextChanged)

VB6 & VB.Net Code Converter:

Why VB6 Upgrading is important:

Magic properties that make many programmer love VB6:

  • Compile on-demand: you could choose to make the source code comply at debug time on-demand only.
  • Edit and continue: In debug mode you not forced to restart your application after errors fix or edit code.
  • Does not need a dot net framework to run.
  • COM and ole compatible.

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 the pre-declared object is not supported in C# or VB.Net.
  • If your application is ActiveX control or ActiveX Document, 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 does a pre-declared object mean:

I wrote a library in VB6 as an add-in in Office VBA the user write the following code when used my lib

Sub Test()
    If Not NP.File.Exists("C:\Temp.txt") Then NP.File.Create "C:\Temp.txt"
    NP.File.Move "C:\Temp.txt", "C:\Temp2.txt"
    NP.File.Copy "C:\Temp2.txt", "C:\Temp.txt"
    NP.File.Delete "C:\Temp2.txt"
    NP.Shell "C:\Temp.txt"
End Sub

The NP object is pre-declared and no need to declare it in VBA In VB.Net of the edition of VBA Extends the user should add a line The user should 1st declare an object named NP before using it.

Public NP As New VBAExtend.NP
Sub Test()
    If Not NP.File.Exists("C:\Temp.txt") Then NP.File.Create "C:\Temp.txt"
    NP.File.Move "C:\Temp.txt", "C:\Temp2.txt"
    NP.File.Copy "C:\Temp2.txt", "C:\Temp.txt"
    NP.File.Delete "C:\Temp2.txt"
    NP.Shell "C:\Temp.txt"
End Sub

Before Using Code Converter:

  • It is better to install VB6, but it is not a must
  • Ensure that you can compile your application could and run it correctly.
  • Ensure that all your referred library are available and work well in the current operating system.
  • Make sure your code can be re-compiled and run well before start converting.
  • No converters can guarantee 100% conversion.
  • Make sure that the dot net language supports your project type.
  • Please note the VB6 converting is hard work because the programing language philosophy is deferring VB6

VB6 Upgrade Companion VBUC:

  • If you are familiar with upgrading VB6, then you will understand how useful this software.
  • It is safe too much of the upgrade time.
  • In the 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

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 a web browser or ActiveX container
  • ActiveX Document

Before Using VBUC:

The VBUC is feather software with many options and we should study before start converting:

  • It is simpler to convert to VB.Net than C#.
  • For ActiveX choose Com Visible
  • Form options Use helper classes whenever available
  • For making your application more like a .net designed to use the More dot net option
  • If the above option cannot 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 an error still appears try to fix them manually.
  • Once you can compile your project, try to follow the warning
  • Switch option explicit on and option strict on and follow any errors appears

VBUC Free version limitation:

It is work for a project with less than 30,000 line code

VB Migration:

  • New powerful VB6 converting application.
  • If you are familiar with upgrading VB6, then you will understand how useful this software.
  • It is safe too much of the upgrade time.
  • Most times 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
  • It’s more simple and faster than VBUC

Properties

  • Supports most major VB6 features
  • Converting in trail software is done on the company server.
  • Fast conversion of large projects.
  • .NET app will look and behaves like the original VB6 code
  • Simple interface
  • Powerful supported libraries
  • Integrated code editor
  • a staged migration is partial conversion is allowed
  • Extendibility
  • The user control will be upgraded.
  • Sub Main in DLL projects & Multi-threaded components
  • Controls in VBMP’s support library expose properties and methods with the same name as the original members, which ensures that code runs correctly even in late-bound scenarios
  • Auto-implemented properties

What VB Migration can’t upgrade

  • Property Page
  • Designer files (*.Dsr)
  • User Controls that used in a web browser or ActiveX container
  • ActiveX Document

You can see Feature Comparison Table

Other VB6 upgrade links

totop