OK, normally I try to be pretty even handed about the whole “VB vs. C#” thing since we’re all one big happy family, but sometimes I just gotta wonder… Scott Wiltamuth (he’s the product unit manager for C#) was talking on his blog about the recent Office System Developer Conference and said:
Given VBA’s long history with Office, one might expect VB .NET to be the primary language among .NET developers using Office as a platform. But I’ve seen a lot of anecdotal evidence that C# usage of Visual Studio Tools for Office (aka VSTO) is high. I’ll try to get some solid data on this and share it here.
Which raises the question: are C# programmers just masochists? I’ve always kind of suspected, what with the case sensitivity, the cryptic syntax, the lack of declarative event handling, and so on. But it seems to me that programming against Office is one of those places where VB has got it over C# hands-down. Sitting on my desk are some chapters of a book that I’m doing technical reviews for. The book is about using Office from managed code, and several of the chapters (maybe the whole book, I haven’t gotten that far) are written in C#. So as I’ve read through the chapters, I just keep circling things and writing in the margin things like “this limitation doesn’t appy to VB” and “this code is simpler in VB.”
For example, VB supports optional parameters, named parameters and passing expressions to reference parameters, whereas C# doesn’t. So when printing, code that in VB looks like:
Doc.PrintOut(Copies := 2, Collate := true)
Becomes in C#:
object copies = 2;
object collate = true;
Doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref copies,
ref missing, ref missing, ref missing, ref collate,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing);
There are other various little gotchas sprinkled throughout Office (like the fact that C# can’t bind to Word’s Close event and VB can, although I think VSTO finesses this a bit), which raises the question: why would you use C# instead of VB to automate Office unless you were a bit of a masochist?
OK, OK, OK, I’ll admit it – I’m only half serious here. I don’t really think that C# programmers are masochists who whip themselves on the back every night chanting “I will initialize all of my variables, I will initialize all of my variables” and wear their “I Am Strongly Typed” hairshirts under their clothes during the day. In fact, I have it on good authority that many C# programmers are normal, well-adjusted members of society. But that doesn’t mean I can’t razz them a bit every once in a while…