C# to VB.NET Code Conversion Web Service

Source Code

Service Description: http://www.kamalpatel.net/ConvertCSharp2VBService.asmx
WinForms Sample Implementation: CS2VB_WinFormsConsumer.zip
VB6 Sample Implementation: CS2VB_VB6Consumer.zip
ASP .Net Online Implementation:

Platform Used

C#, ASP .Net, VB6, SOAP Toolkit 2.0

Article Description

The title says it all. ConvertCSharp2VB is a Web Service that converts a C# code block into VB.Net. It exposes the Service Description and WSDL for the Web Service, so you can implement this functionality in your applications.

In this article I will begin by explaining how we can create a Web Service Consumer using C# to access the ConvertCSharp2VB Web Service. Later on I also demonstrate writing a Consumer that accesses this service using VB6 and SOAP Toolkit 2.0.


ConvertCSharp2VB is small utility class developed using C#. The class exposes only one public method, Execute() that receives a C# code block as a parameter and returns its equivalent VB.Net code block. When the Execute() method is called, the class looks for specific patterns in the C# code block and converts each code block to its VB.Net equivalent. The Web Service ConvertCSharp2VBService is simply a wrapper around an instance of this class.

The converter currently handles most of the conversions from C# to a VB.Net. It handles namespaces, classes, structs, enums, methods, properties, fields, declaration, for-loops, do-while, while-do, foreach, if-else-end if, select-case, try-catch-finally etc. to name a few.

Creating a Consumer in .Net

To access the web service from your project, right click on the project in Solution Explorer and select Add Web References. Type the following URL http://www.kamalpatel.net/ConvertCSharp2VBService.asmx and select Add Reference. 


This will add the web reference to your project and create all the necessary plumbing code to access this Web Service. Once the service is added to your project, you can access it just like any other class reference. Here is the code that is required to access this service.  

private void cmdConvert_Click(object sender, System.EventArgs e)
//Instantiate the service
net.kamalpatel.www.ConvertCSharp2VBService oService;
oService =
new net.kamalpatel.www.ConvertCSharp2VBService();
//Call the execute method and handle the formatting
string lcStr = o.Execute(this.txtCSharp.Text);
this.txtVB.Text = lcStr.Replace("\n", "\r\n");

Just like any other object we create an instance of the web service class (net.kamalpatel.www.CpnvertCSharp2Vbservice) and call its methods. In our case we call the Execute() method that receives a CSharp code block and returns a VB.Net code block. Here is an image that shows the implementation of this Web Service developed using ASP.Net.


Consumer in VB6

Here is a typical code for consuming a Web Service from VB6. We begin by instantiating the SOAP Client and initialize it by specifying the location of our WSDL.  Once the SOAP client is initialized, it allows us to call methods of our component as if it were a part if the SOAP Client object. In this case notice that Execute() is actually a method of the Web Service. 

Private Sub cmdConvert_Click()
'Create the SOAP Client
Dim soapClient
soapClient = CreateObject("MSSOAP.SoapClient")
'Initialize the soap client and pass the URL for
'the WSDL file as a parameter
Dim cWSDL As String
cWSDL = http://www.kamalpatel.net/ConvertCSharp2VBService.asmx?WSDL
Call soapClient.mssoapinit(cWSDL)
'Call the Execute() method and display the results
Dim cRetVal As String
cRetVal = soapClient.Execute(Me.txtCSharp.Text)
Me.txtVB.Text = Replace(cRetVal, vbLf, vbCrLf)
End Sub

Internally, the soap client determines the method and makes the call to the actual service.  The last line in the above code block replaces all the line feeds with carriage returns + line feeds. This makes it more readable when displayed in the textbox. 


The converter does a great job for VB developers as most of the .Net code is still in C#. Even though not 100% complete, it supports 90% of the available types.

This Web Service is free to use and access, so if you wish to create consumers on your web site that access this service, it will all yours. If you simply wish to use the service, you can access it from http://www.KamalPatel.net/ConvertCSharp2VB.aspx.