Convert C# to VB.NET


There are various ways to converting CSharp to Visual Basic .Net . I explain about to to convert C# to VB.NET

This sample shows how to programmatic convert C# code to VB.NET and vice-versa. I am using the sharpdevelop's ICSharpCode.NRefactory namespaces here to convert code.

You can download the assemblies from http://www.icsharpcode.net/OpenSource/SD/Download

The first parameter of the method is the actual code that need to be converted. Second parameter specifies whether to convert from C# to VB.NET or vice versa. Pass 'true' to convert from C# to VB.NET. Pass 'false' to convert from VB.NET to C#.


Private Function ConvertCodeSnippet(ByVal codeToConvert as string, ByVal csharpToVb As Boolean) as string
Dim input As New StringReader(codeToConvert)
Dim parser As IParser

parser = ParserFactory.CreateParser(IIf(csharpToVb, SupportedLanguage.CSharp, SupportedLanguage.VBNet), input)

parser.Parse()

If (parser.Errors.count > 0) Then
return parser.Errors.ErrorOutput
End If

Dim cu As CompilationUnit, output As IOutputASTVisitor
cu = parser.CompilationUnit

output = IIf(csharpToVb, CType(New VBNetOutputVisitor(), IOutputASTVisitor), CType(New CSharpOutputVisitor(), IOutputASTVisitor))

cu.AcceptVisitor(output, DBNull.Value)
return output.Text
End Function


Comments

Author: Kumar06 May 2009 Member Level: Silver   Points : 1

Nice Code.
Sir , Like this can we successfully Convert VB6.0 Code
to VB.NET 2005 code ?

Author: D.Jeya kumar(JK)07 May 2009 Member Level: Gold   Points : 1

Hi,


Good one. Nice to see a simple code converter to convert c# to vb.net and viceversa

Regards
JK

Guest Author: 19 Mar 2012

nice one

Author: Sarang Singnapurkar30 Jun 2012 Member Level: Gold   Points : 0

Hi,
This code is simple to use.
Article is very informative.
Thanks for sharing.

Author: Shan.R02 Jul 2012 Member Level: Silver   Points : 2

Hi




private string ConvertCodeSnippet(string codeToConvert, bool csharpToVb)
{
StringReader input = new StringReader(codeToConvert);
IParser parser;

parser = ParserFactory.CreateParser(IIf(csharpToVb, SupportedLanguage.CSharp, SupportedLanguage.VBNet), input);

parser.Parse();

if ((parser.Errors.count > 0)) {
return parser.Errors.ErrorOutput;
}

CompilationUnit cu;
IOutputASTVisitor output;
cu = parser.CompilationUnit;

output = IIf(csharpToVb, (IOutputASTVisitor)new VBNetOutputVisitor(), (IOutputASTVisitor)new CSharpOutputVisitor());

cu.AcceptVisitor(output, DBNull.Value);
return output.Text;
}


Regards,
Shan.R



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: