Click to Rate and Give Feedback
Related Articles
Here the author introduces SQL Server Data Services, which exposes its functionality over standard Web service interfaces.

By David Robinson (July 2008)
Here the author answers questions regarding the Entity Framework and provides an understanding of how and why it was developed.

By Elisa Flasko (July 2008)
Here we present techniques for programmatic and declarative data binding and display with Windows Presentation Foundation.

By Josh Smith (July 2008)
Systems that handle failure without losing data are elusive. Learn how to achieve systems that are both scalable and robust.

By Udi Dahan (July 2008)
More ...
Popular Articles
Jay Flowers demonstrates how to set up and use a Continuous Integration server using both discrete tools and the more comprehensive CI Factory solution.

By Jay Flowers (March 2008)
Mike Volodarsky demonstrates the IIS 7.0 extensibility model by extending the Response Modification into a configurable Web server module and a custom management page for IIS Manager.

By Mike Volodarsky (Launch 2008)
Here the author uses Document Information Panels in the Microsoft 2007 Office system to manipulate metadata from Office docs for better discovery and management.

By Ashish Ghoda (April 2008)
Joel Pobar presents an introduction to how compilers work and how you can write your own compiler to target the .NET Framework.

By Joel Pobar (February 2008)
More ...
Read the Blog
SQL Server Data Services (SSDS) is a robust, scale-free data service that internally uses proven SQL Server technology and exposes its functionality over industry standard Web service interfaces. In the July 2008 issue of MSDN Magazine, David Robinson introduces ...
Read more!
Windows Presentation Foundation (WPF) offers excellent support for managing the display and editing of complex data. In the December 2007 edition of MSDN Magazine, John Papa did a great job of explaining essential WPF data binding concepts. ...
Read more!
The most fundamental form of Web testing is HTTP request/response testing. This involves programmatically sending an HTTP request to the Web application, fetching the HTTP response, and examining the response for an expected value. In the May 2008 issue of MSDN Magazine, Read more!
In the November issue of MSDN Magazine, Jeffrey Richter demonstrates some recent additions to the C# programming language that make working with the APM significantly easier. In the June ...
Read more!
The July 2008 issue of MSDN Magazine is now available online. Here's what's in the issue: Data Services: Develop ...
Read more!
The June 2008 issue features the first installment of a new MSDN Magazine column on software design fundamentals. We’ll discuss design patterns and principles in a manner that isn't bound to a specific tool or lifecycle methodology. In this issue, Jeremy Miller starts the Patterns in Practice column ...
Read more!
More ...
Paste As Visual Basic
A Visual Studio Add-In That Converts C# Code To Visual Basic
Scott Swigart

This article discusses:
  • Using code conversion Web services
  • Unit testing code
  • Creating a Visual Studio add-in
  • Adding menu items to Visual Studio
This article uses the following technologies:
Visual Studio 2005, Visual Basic .NET
The "Paste as..." functionality in applications like Microsoft® Word has become indispensable for me. I often copy something from the Web and want to paste it into a document without all of the HTML formatting. Paste as is the tool for the job.
While perusing the Web one day, looking for a code example, it occurred to me that Paste as would also be beneficial for Visual Studio®. However, instead of simply converting the text formatting, it could convert the language of the code example. It's common to be coding in C# and find a Visual Basic® example that does exactly what you want, or vice versa. Using such code requires copying the sample to the clipboard, going to one of the many great code conversion Web sites, pasting in the code, converting it, copying the new code, and finally pasting the converted code into Visual Studio.
When I find a C# code example on a Web site, I'd really like to eliminate the intermediate conversion steps and just Paste as Visual Basic right into the code editor. All the necessary building blocks are in place to make this work; I just need some code to glue it all together.

Creating the Add-In
There are a number of Web-based code converters available, so I wanted to architect the Paste as Visual Basic add-in so that the user can choose whichever converter he wants to use. When you're dealing with Web solutions, there's no guarantee that a particular Web site will remain operational indefinitely, or that it won't change its implementation in a way that breaks your code. Hooking up to more than one conversion service makes it more likely that there will always be a service available to perform the conversion. Also, different code converters use different algorithms, and one may work better or worse for a particular piece of code. Finally, I wanted it to be easy for the Visual Studio add-in to support additional converters in the future.
To abstract the converter implementation from the rest of the code, I crafted the IConvertCode interface. This describes the conversion operation in general, but will leave the specific implementation to other classes. The IConvertCode interface is pretty simple:
Public Interface IConvertCode
    Function Convert(ByVal csCode As String) As String
    ReadOnly Property ConverterName() As String
End Interface
The Convert method takes C# code as a string argument and returns Visual Basic code as a string. The ConverterName property returns the name of the converter so that the user can choose the converter that they would like to use.
The first implementation of this interface uses Kamal Patel's C# to Visual Basic code converter Web service, currently available at ConvertCSharp2VB. Once a reference is added to this service, the code to call it is very straightforward, as shown in the code in Figure 1.
The Web service exposes an Execute method that takes C# code and returns Visual Basic code. Since the Web service just happens to use the same signature as IConvertCode.Convert, the arguments to IConvertCode.Convert can just be passed through to the Web service, and the results can be returned.
Another great tool is Carlos Aguilar Mares's AJAX-powered code converter, available at CodeTranslator. This converts between C# and Visual Basic as you type. Behind the scenes, the code is posted to carlosag.net/Tools/CodeTranslator/translate.ashx as a set of form fields. Another implementation of IConvertCode uses this facility to perform translation, as shown in Figure 2.
This implementation posts the C# code as form fields to a Web address. The Visual Basic code is returned as the body of the HTTP response. To send the C# code, a NameValueCollection is used. The C# is added as the code item, the source language is specified as the Language item, and the Visual Basic result is specified as the DestinationLanguage item. Once the collection is populated, it is then sent to the Web address using the WebClient.UploadValues method. This method posts the form fields and gets the results back as an array of bytes. Encoding.ASCII.GetString is used to convert the array of bytes back to a string. This string contains the Visual Basic translation.
You can see that although the internal implementations are quite different, both converters satisfy the IConvertCode interface in that they provide methods that take C# code as a string and return Visual Basic code as a string.

Testing Times
I've placed the implementations of code conversion into a class library project. In the past, I probably would have created a simple Windows Form application to test my two converters to make sure they're functioning properly. However, Visual Studio 2005 Team Edition for Software Developers provides built-in unit testing capabilities. All I have to do is right-click inside of a method and I can quickly generate a test for that method.
The first time you create a unit test, a new project is added to your solution and the test code is placed in that project. The unit test for one of the conversion methods is shown in Figure 3.
The test loads the C# source code from a text file. The code is converted to Visual Basic and compared with another file that contains the expected results. If the results match, then the test passes. The Test Manager window lets me quickly run my unit tests. If I add more converters in the future, or make changes to my existing logic, these tests should give me a quick way to make sure that everything is working properly.

Adding an Add-In
Now that I know the converters are working as expected, it's time to create an add-in for Visual Studio 2005. You create an add-in by adding a new add-in project to your solution. Add-ins are available under Extensibility projects, as shown in Figure 4.
Figure 4 Creating an Add-In Project 
The wizard walks you through a number of questions to assist in building the skeleton for the add-in. You're prompted to specify a name, whether you want the add-in to show up in the toolbar, whether the add-in should load when Visual Studio starts, and whether it's always modeless so that it's safe to use from command-line builds.
Unlike in Visual Studio .NET 2003, the add-in project for Visual Studio 2005 contains the boilerplate code needed to correctly connect your add-in into the development environment. The add-in class implements IExtensibility2, which is the interface that Visual Studio will use to communicate with the add-in. Some key methods, like OnConnection and QueryStatus, are also written for you. The Exec method—the method that's called when the user invokes your add-in—is stubbed out, too.
When you set the add-in as the start-up project and press F5, an amazing thing happens: a new instance of Visual Studio launches with the add-in installed. You can use this new instance of Visual Studio to test the add-in. You can even set breakpoints and otherwise debug your add-in just like any other piece of code. Launching the add-in in a test instance of Visual Studio is a new and very welcome feature in Visual Studio 2005.
There are a couple changes that I want to make to the boilerplate at this point in the process. First, the add-in shows up under the Tools menu by default. I really want it to show up under the Edit menu, right after the Paste menu item. The code in Figure 5 shows the modified OnConnection method.
There are a few important modifications made to the boilerplate for this method. First, the project includes a resource file called CommandBar.resx, which contains the text for the top-level Visual Studio menu items in many languages. The following line of code looks up a menu item, prefixed with your two-letter language name:
editMenuName = resourceManager.
    GetString( _
    String.Concat(cultureInfo.TwoLetterISOLanguageName, "Edit"))
If you're running this on a computer set for U.S. English, it looks up a resource string named enEdit in CommandBar.resx. If the computer was configured for German, it would look up deEdit. This will return culture-specific text for the menu item, which is needed to locate that menu object programmatically. Once the top-level menu is located, you can add submenus to it. The following lines of code locate the actual menu object:
Dim editControl As CommandBarControl = _
    menuBarCommandBar.Controls.Item(editMenuName)
Dim editPopup As CommandBarPopup = CType(editControl, CommandBarPopup)
Once the menu bar is located, the new Paste as Visual Basic menu item can be added to it. This is done by creating a command and then adding the command to the menu:
Dim command As Command = commands.AddNamedCommand2( _
    _addInInstance, "PasteAsVBAddIn", "Paste as Visual Basic", _
    "Convert the clipboard content from C# to Visual Basic and paste", _
    True, 59, Nothing, _
    CType(vsCommandStatus.vsCommandStatusSupported, Integer) + _
    CType(vsCommandStatus.vsCommandStatusEnabled, Integer), _
    vsCommandStyle.vsCommandStylePictAndText, _
    vsCommandControlType.vsCommandControlTypeButton)

command.AddControl(editPopup.CommandBar, 12)
The command is added to the menu at position 12, which places it right after the Paste menu item (see Figure 6).
Figure 6 Add-In on Edit Menu 
It's also important that the add-in only be enabled when it could conceivably be used. You would only want to Paste as Visual Basic if you were actually editing a Visual Basic code file. Enabling and disabling the add-in is handled by the QueryStatus method. The code shown in Figure 7 will cause the add-in to display only when there's an active document that ends with the extension .vb, and there's text on the clipboard.
At this point in the project, the add-in is properly wired into Visual Studio and nearly ready to be used to convert the code. The only remaining task is to connect the Exec method of the add-in to the code that will actually perform the conversion and paste the results into the editor.

Form of Conversion
In order to provide a rich experience for the user, I've created a form that allows the user to select from various conversion options and preview the conversion results. Figure 8 shows the conversion form that's displayed when the user selects the Paste as Visual Basic menu command.
Figure 8 Conversion User Interface 
The C# source area shows the current contents of the clipboard. A listbox allows the user to choose the converter that they want to use. Clicking the Preview button will cause the output of the conversion to be shown at the bottom of this form, to insure that the conversion will be acceptable before pasting it into your code file. Clicking Convert will perform the conversion and paste the results directly into the open code file. If the Format Code After Convert checkbox is checked, then the code will be reformatted to insure proper indenting after the converted code is pasted in.
The code for the form is pretty simple and uses the conversion classes that were shown at the beginning of this article:
Private converters() As IConvertCode = { _
    New PasteAsVB.CAConvert(), _
    New PasteAsVB.WSConvert() _
    }

Private Sub ConverterForm_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    CsSourceTextBox.Text = My.Computer.Clipboard.GetText
    ConverterListBox.ValueMember = "ConverterName"
    ConverterListBox.DisplayMember = "ConverterName"
    ConverterListBox.DataSource = converters
End Sub
When the form loads, an array of converters is instantiated. These converters all implement the IConvertCode interface. In FormLoad, the listbox is bound to this array, and the ConvertName property is set to be displayed. This causes the name of each converter to appear in the listbox. If you wanted to add more converters, you would just create additional classes that implement IConvertCode, and then add them to the converters array. This could also be modified to pull the list of converters to be used from a configuration file.
When Preview is clicked, the selected converter is used to perform the conversion and display the results in the preview textbox. When Convert is clicked, the converted code is assigned to a property of the form, and the form closes. The code for the preview and convert functionality is shown in Figure 9.
The conversion form is now complete and simply needs to be invoked from the Exec method of the add-in as shown in Figure 10. When the add-in is invoked, the conversion form is displayed. If the Convert button is clicked, the conversion is performed, and the results are stored in the VBCode property of the form. The form then returns with a result of DialogResult.OK. The add-in checks the result and then inserts the converted code into the code editor. If the format check-box is checked, the add-in also executes the Edit.FormatDocument command to reformat the current code file.

Conclusion
Visual Studio 2005 simplifies the task of creating add-ins. First, it generates boilerplate code that will hook your add-in into the menu of Visual Studio. Second, when you press F5, a test instance of Visual Studio will be launched that contains your add-in. This makes debugging your add-in very simple.
The add-in shown here performs a very useful task—it allows you to paste C# code into your project as Visual Basic code. The add-in includes the ability to use two existing Web-based converters, but it's constructed in such a way that additional converters would be easy to add.
If you just want to install this add-in and go, the code download accompanying this article (on the MSDN Magazine Web site) includes an installer project that will let you install Paste as Visual Basic into any Visual Studio 2005 development environment. I hope that you see that creating your own add-ins doesn't need to become a daunting task. If you find yourself saying, "I wish I had an add-in that accomplished...," you just might be able to build that add-in yourself more easily than you may think.

Scott Swigart spends his time consulting, writing, and speaking about converging and emerging technologies. Scott is the author of several books on .NET, a certified Microsoft trainer (MCT) and developer (MCSD), and a Microsoft MVP. You can contact Scott at scott@swigartconsulting.com.

Page view tracker