Ziff Davis EnterpriseDevLife
Advertisement
Advertisement

Sunday, September 07, 2008 11:36 AM/EST

Tangible Software's Instant VB/C# converters won my heart in one line of code

Quite often you'll find that perfect code sample to fit a need, but it's in that other language. Generally, I can translate with no problems even if it takes some time. But occasionally I'll happen upon a use of .NET that I'm unfamiliar with, but the syntax difference between C# and VB are so different that I have no clue how to translate it.

This happened recently with a great sample app written by Tony Sneed of Developmentor which he used in his TechEd US Developer presentation, Real World LINQ to SQL.

Tony had written a helper class to customize collection behavior in a WCF Client. The class implements the ObservableCollection .NET class. The custom collection will be used generically, but can only be used for classes that implement particular interfaces. In other words, by being generic, it can be a collection of any type; but adding constraints you can define the collection to work with only certain types .

It was the "s" at the end of "interfaces" that stumped me.

Here's what the class declaration looks like in C#.

 public class ChangeTrackingCollection<T> : ObservableCollection<T>
        where T : ITrackable, INotifyPropertyChanged
    {} 

The collection inherits from ObservableCollection<T> and can only be built from classes which implement ITrackable (a custom interface) and INotifyPropertyChanged.

In VB, I'm familiar with applying constraints to generics. But only a single constraint.

Public Class ChangeTrackingCollection(Of T As INotifyPropertyChanged)
    Inherits ObservableCollection(Of T) 

It took me about 45 minutes of fiddling around, then reading online until I finally realized that the term I needed to focus on was "constraint" and then by searching for "multiple constraints" with generics, I easily found an example of how to force multiple constraints in VB right in the MSDN documentation.

The proper syntax is:

Public Class ChangeTrackingCollection(Of T As {INotifyPropertyChanged,ITrackable})
    Inherits ObservableCollection(Of T)

Somehow after all of this, I remembered that so many have recommended Tangible's Instant VB and C# applications for converting. So I downloaded the demo for InstantVB, fed it the C# above and yes, Instantly, it converted to the correct syntax in VB. That's way faster than 45 minutes.

I'm sold.

By the way, Charles Petzold just published an article on ObservableCollection in the September Issue of MSDN Magazine: Dependency Properties And Notifications

TrackBack

TrackBack

http://blogs.devsource.com/cgi-bin/mte/mt-tb.cgi/14836

Comments (6)

Another trick is to compile the C# or VB.NET code, then fire up .NET Reflector over it and decompile it to the other language :)

Also does Managed C++ and Delphi and is, of course, free.

[)amien

How about using:
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx

for free???? So far its done a pretty good job.

Ran your code through it and it output the same results.

Kim :

http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx is probably the quickest way to do ad-hoc conversions. Supports both C#2VB.NET and VB.NET2C#

Richard Howells :

The SharpDevelop, free IDE, can do this kind of thing too. You can take a whole project in the second best .Net programming language; right click; and choose convert to the best .Net programming language

julie :

Okay, okay! :-)

So the point is, there's no need to waste time doing the conversions manually(outside of the great value gained from increasing your skills in various langauges, of course!). There are tools out there that can help. I happen to have already (and happily) chosen Tangible's based on the many recommendations I had for it.

Bill :

A lot of people say goods things about Tangible. Maybe we can take up a collection and buy MS a copy ;-)

Post a Comment

 
 

Advertisement

Syndication

Subscribe: