Converting C# samples to VB.NET
It seems that all of the good samples out there for Silverlight or any other .NET technology are written in C#. So f you’re a VB.NET developer, what are you to do?
There are some good tools out there to convert C# code to VB.NET, some are very good and cost a good deal of money. For a quick and dirty conversion of pieces of code, I have found the following to work pretty well.
You may have already used Red Gate’s Reflector application (formerly Lutz Roeder’s Reflector), downloadable for free here:
http://www.red-gate.com/products/reflector/
Reflector is typically used for digging into assemblies that you don’t have the source code for and figuring out how they work. However, by using the language dropdown, you can get a pretty decent language converter.
Let’s assume you have a sample written in C#. For this example, I’ll use a piece of code from our upcoming book, Hello! Silverlight 2. As a side note, you won’t have to use a method like this for converting the samples in our book since we will be including all of the samples as both C# and VB.NET inline in the book’s text.
If we select the dll output from a Silverlight project, and then select one of the classes in it, let’s say FishEyeMenuItem in our case, you’ll see the following:
Then you can click on the Expand Methods link in the right hand pane, and see all of the C# source code for this class:
Now if you look in the top toolbar, you’ll see a dropdown that says C#. Change that to “Visual Basic” and Expand Methods again and you’ll see something like this:
Now since this is code for a user control, there is generated code mixed in here, specifically the InitializeComponent method and some generated field definitions corresponding to XAML elements. So if you try to copy and paste this into a VB version of the project you would get duplicate definitions. It’s pretty easy to track those down and remove them though, and the code then should work as-is.