Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Code Convert Visual Studio Add-in for C# and VB.NET

0.00/5 (No votes)
20 Nov 2007 1  
Adds a convert from C# to VB.NET and from VB.NET to C# button to the tools menu

Introduction

This solution contains an add-in project for Visual Studio.

The add-in adds a button to the tools menu for converting VB.NET code from and to C#.

The solution also contains a set up project for the add-in.

Background

I created this project as part of my efforts to study add-in programming. I chose code conversion because I couldn't find one that enables conversion from C# to VB.NET and from VB.NET to C# easily enough.

The add-in uses an external service which can be found here.

Screenshot - ctovb.jpg

You can easily change the provider or use local code (if you have it - I couldn't find a good example for a local converter and that is why I used the external service).

Note: You must be connected to the Internet in order to use the add-in while it's using the external service.

Using the Code

This is the Exec method which runs whenever the user hits the button:

//
public void Exec(string commandName, vsCommandExecOption executeOption, 
		ref object varIn, ref object varOut, ref bool handled)
    {
        try
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName == "CodeConvert.Connect.CodeConvert")
                {
                    handled = true;
                    string url = 
		       "http://www.carlosag.net/Tools/CodeTranslator/Translate.ashx";
                    string parameters = String.Empty;
                    string remark = String.Empty;
                    string code = Clipboard.GetData(DataFormats.Text).ToString();
                    if (_applicationObject.ActiveDocument.Name.EndsWith("cs"))
                    {
                        remark = "//C# converted code, conversion by: 
                        	http://www.carlosag.net/Tools/CodeTranslator/ " + 
			                Environment.NewLine;
                        parameters = "Code=" + System.Web.HttpUtility.UrlEncode(code) + 
				"&Language=VB&DestinationLanguage=C#";
                    }
                    else if (_applicationObject.ActiveDocument.Name.EndsWith("vb"))
                    {
                        remark = "'VB.NET converted code, conversion by: 
			            http://www.carlosag.net/Tools/CodeTranslator/" + 
			            Environment.NewLine;
                        parameters = "Code=" + System.Web.HttpUtility.UrlEncode(code) + 
				"&Language=C#&DestinationLanguage=VB";
                    }
                    if (remark != String.Empty)
                    {
                        string result = HttpWebRequestHttpPost(url, parameters);
                        ((TextSelection)_applicationObject.ActiveDocument.Selection)
                               .Text = remark + result + Environment.NewLine; ;
                    }
                    else
                    {
                        MessageBox.Show
			("CodeConvert can be used for .vb or .cs files only");
                    }
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error:" + ex.Message);
            return;
        }    
    }
// 

The add-in knows what to convert to by the current file extension - so when you want to convert VB.NET to C#, you need to be in a *.cs file pasting VB.NET code and when trying to convert C# to VB.NET, you need to be in a *.vb file pasting C# code (pasting -> hitting the button with the original code in your clipboard).

Example

Screenshot - vb.jpg

Figure 1: Original VB.NET Code (Copy).

Screenshot - csbefore.jpg

Figure 2: Tools Menu.

Screenshot - csafter.jpg

Figure 3: C# code is pasted in the target file.

Enjoy!

History

  • 20th November, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here