C To Vb.net

-->

Paste As Visual Basic

A Visual Studio Add-In That Converts C# Code To Visual Basic

Convert VB.NET to C# and C# to VB.NET Free online code conversion tools Write code in your favourite language and convert to any.NET language you want! We are presenting several online tools to convert.NET code snippets between various programming languages. VB.NET and C# are syntactically different programming languages with different history but both the languages are part of the.NET framework development platform and maintained by Microsoft. Both these.NET languages share the same run-time engine and have platform specific features such as cross language inheritance, garbage collection. Welcome to VB.NET section of C# Corner. In this section, you will find various VB.NET related source code samples, articles, tutorials, and tips using C# language. Learn VB.NET programming with C# Corner.

Scott Swigart

Convert VB.NET to C# and vice versa with this roslyn based converter. Rating & Review. Adds context menu items to convert projects/files between VB.NET and C#. Flexible: Convert a small selection, or a whole solution in one go, in either direction. Accurate: Full project context (through Roslyn) is used to get the most accurate conversion. C# is case sensitive while Visual Basic.NET is not. Thus in C# it is possible to have two variables with the same apparent name, for example variable1 and Variable1. Visual Studio will correct (make uniform) the case of variables as they are typed in VB.NET. In some cases however, case sensitivity can be useful.

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

Code download available at: Paste As.exe(875 KB)

Contents

Creating the Add-In
Testing Times
Adding an Add-In
Form of Conversion
Conclusion

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:

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.

Figure 1 Calling a Conversion Web Service

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.

Figure 2 Using a Form-Based Converter

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.

C To Vb.net

Figure 3 Unit Test for Form-Based Conversion

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.

Figure 5 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. Typing tutor c program. The following line of code looks up a menu item, prefixed with your two-letter language name:

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:

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:

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.

Figure 7 Control Whether Add-In is Enabled Download ebook psikologi perkembangan remaja. Xforce keygen online.

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:

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.

C3 To Vb.net

Figure 9 Preview and Convert Functionality

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.

Figure 10 Performing a Conversion

C To Vb.net Code Converter Online

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.

C# To Vb.net Format

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.