John Papa [MVP C#]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Trying Code for my Book in VB and C#

I've decided to at least try to put all of the code sample in my Data Access with Silverlight 2 book in both VB and C#. While VB was once my main language, C# has long since been the primary language I use.  Thankfully, there is the tool from Tangible Software Solutions called Instant VB that converts C# code to VB code. It can convert a snippet of code, a file, or an entire solution. Its pretty darn fast too.

For example, here is a class I created in C# ...

public class Customer
{
    private int id;
    private string companyName;
    private Address companyAddress;
 
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
 
    public string CompanyName
    {
        get { return companyName; }
        set { companyName = value; }
    } 
 
    public Address2 CompanyAddress
    {
        get { return companyAddress; }
        set { companyAddress = value; }
    } 
}

and here is the code that Instant VB created for me.

Public Class Customer
    Private privateID As Integer
    Public Property ID() As Integer
        Get
            Return privateID
        End Get
        Set(ByVal value As Integer)
            privateID = value
        End Set
    End Property
    Private privateCompanyName As String
    Public Property CompanyName() As String
        Get
            Return privateCompanyName
        End Get
        Set(ByVal value As String)
            privateCompanyName = value
        End Set
    End Property
    Private privateCompanyAddress As Address
    Public Property CompanyAddress() As Address
        Get
            Return privateCompanyAddress
        End Get
        Set(ByVal value As Address)
            privateCompanyAddress = value
        End Set
    End Property
End Class

While this small snippet is easy enough to translate, it really comes in handy when you forget an END statement or a line continuation character or some other language specific syntax (or a squiggly bracket in C#).

So far its going pretty well as I have only run into a few places where it did not convert the code as I expected it would. But it was easily fixed and I suspect that I had a problem because I was just converting a snippet of code (not even an entire method) and the tool could not determine if it was a property, method, or what. As I continue writing the book I will post more on my experiences with this tool, but so far its been pretty good. I don;t know if I trust it to convert my entire solution just yet, but I will be giving it a try once I have time to go through it all and test it.

They also have a Instant C# tool that converts from VB to C#, too.

And yes, this tool is not free :)

 

Cross posted from johnpapa.net


Posted Mon, May 19 2008 1:06 PM by John Papa
Filed under: ,

[Advertisement]

Comments

Chris Brandsma wrote re: Trying Code for my Book in VB and C#
on Mon, May 19 2008 1:19 PM

While not as spiffy as that commercial tool, I find that Reflector can translate most code quite easily as well.

But to do it you first have to make sure your code compiles.  But once loaded you can translate into C#, VB.Net, Delphi.Net, IL, and there is even a parser for Boo (but I haven't had great luck getting that one to work).

John Papa wrote re: Trying Code for my Book in VB and C#
on Mon, May 19 2008 1:29 PM

Yes, and Reflector is free too :)

Mike Strother wrote re: Trying Code for my Book in VB and C#
on Mon, May 19 2008 2:11 PM

Telerik has a free code converter, which I have used; it works pretty well.

http://converter.telerik.com/

John Papa wrote re: Trying Code for my Book in VB and C#
on Mon, May 19 2008 2:36 PM

Thanks Mike. Yeah, that works too for snippets. Instant VB seems to have an edge when converting an entire solution. But if you just need a snippet, there are a lot of tool sout there fr free.

Jeff Certain wrote re: Trying Code for my Book in VB and C#
on Mon, May 19 2008 4:17 PM

I'd be interested in seeing whether the code is simply a translation of the C#, or writes code like a competent VB coder really would.

e.g.

Dim sb As New StringBuilder(256)

vs

Dim sb As StringBuilder = New StringBuilder

Dew Drop - May 20, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - May 20, 2008 | Alvin Ashcraft's Morning Dew
on Tue, May 20 2008 12:34 PM

Pingback from  Dew Drop - May 20, 2008 | Alvin Ashcraft's Morning Dew