17 lines of C# to 1 line of VB.Net
I'm part of a team doing a presentation on MS Office and Web Service integration next week, and while I generally prefer programming in c#, when doing this MS Office work and using the Visual Studio Tools for Office, I prefer VB.Net. Check out the VB.Net syntax to open a file compared to C#:
' Visual Basic
ThisApplication.Documents.Open( "C:\Test\Test.doc" )
// C#
Object filename = @"C:\Test\Test.doc" ;
Object confirmConversions = Type.Missing ;
Object readOnly = Type.Missing ;
Object addToRecentFiles = Type.Missing ;
Object passwordDocument = Type.Missing ;
Object passwordTemplate = Type.Missing ;
Object revert = Type.Missing ;
Object writePasswordDocument = Type.Missing ;
Object writePasswordTemplate = Type.Missing ;
Object format = Type.Missing ;
Object encoding = Type.Missing ;
Object visible = Type.Missing ;
Object openConflictDocument = Type.Missing ;
Object openAndRepair = Type.Missing ;
Object documentDirection = Type.Missing ;
Object noEncodingDialog = Type.Missing ;
ThisApplication.Documents.Open( ref filename,
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDocument, ref passwordTemplate, ref revert,
ref writePasswordDocument, ref writePasswordTemplate,
ref format, ref encoding, ref visible, ref openConflictDocument,
ref openAndRepair , ref documentDirection, ref noEncodingDialog ) ;
Is my point clear? 17 lines of c# to 1 line of VB.Net. C# becomes a very verbose way of working with the object models -- this isn't just for the Documents.Open method, it's everywhere. Of course, one could write a helper object to handle these Type.Missing parameters, but a truly universal helper object would have overloads with every reasonable combination -- something I certainly won't waste my time doing just to say “This was done in C#.“ Microsoft doesn't provide a helper object, either, so they figured it wasn't worth their time. I won't even discuss the get_Item syntax in C# for accessing Office collections . . .
Divergence between C# and VB.Net is supposed to be on it's way . . . future releases of the language and editor will make C# a tool more for component and system developers and VB.Net a tool more for rapid application development . .. but in the world of Office development there's already a big difference between the two languages! If you're getting paid by the number of lines of code, then C# wins hands down!
Pick the right tool for the job and use VB.Net for Office development! If you want to attend the presentation, get to the www.WeProgram.Net meeting next Tuesday night.
Happy .Netting!