COM interop improvements with C#4.0

I already did a blog post about this some time ago: Better Office COM interop thanks to named and optional parameters

Here I talked mainly on how named and optional parameters make life easier for Office developers. This focus was more on that. But actually there are a some other points that didn’t get enough attention. Here are some other great improvements specifically for COM interop.

  • No more meaningless ref parameters.
  • Better indexer support, property accessing. No specific get/set needed anymore.
  • No more need for casting, we can use dynamic binding thanks to the Dynamic class.

I have found a screencast by Sam Ng who explains all this in about 10min.


Link to the original post –> How Do I: COM Interop and Office in C# 4.0?

Better Office COM interop thanks to named and optional parameters

In my last screencast I showed you how to work with two new features in C#4.0 which are named and optional parameters. Now in this post I will show you another way how you can benefit from these new features.

Remember when you had to create these Microsoft Word document with the Office interop API and had to pass all these tedious by ref parameters. Things you absolutely didn’t care about! But anyway the Office COM Interop assemblies were expecting them. Thanks to named and optional parameters this will all go away!

So let’s first start with adding a reference to the Microsoft Office Word Interop assembly.

image

Following is the code we had to write before we could use named and optional parameters.

class Program
{
   static void Main(string[] args)
   {
       object missing = System.Reflection.Missing.Value;
       object visible = true;

       //Create instance of Word application
       Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
       //Make the Word application visible
       wordApp.Visible = true;
       //Create instance of Word document
       Word.Document wordDocument;

       //Add document to document collection of word instance
       wordDocument = wordApp.Documents.Add(ref missing, ref missing, ref missing,ref visible);
       //Type text in our Word document
       wordApp.Selection.TypeText("This is my dynamically created word document");

       Console.WriteLine("Done");
       Console.In.ReadLine();

   }
}

As you can see we had to define this missing object and pass it when we added our Word document to the documents collection of the Microsoft Word application instance. This doesn’t mean that much but hey we had to add it anyway to make it work. Now with optional parameters this isn’t needed anymore. We can just leave them out!

wordDocument = wordApp.Documents.Add(ref visible);

Will this do the trick then? Well not really because now you have removed 3 missing objects and are left with the visible Boolean. To make it really work, we have to define which parameter the visible object represents. This can be done with the named parameter feature. Now the method call will look like this:

wordDocument = wordApp.Documents.Add(Visible:ref visible);

Isn’t this so cool? But wait it gets even better. Another new feature of C# 4.0 is that we don’t need to explicitly pass parameters by reference anymore. So we can clean our code up some more. Finally we will end up with this piece of code, which is by the way much more readable.

class Program
{
   static void Main(string[] args)
   {
       //Create instance of Word application
       Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
       //Make the Word application visible
       wordApp.Visible = true;
       //Create instance of Word document
       Word.Document wordDocument;

       //Add document to document collection of word instance
       wordDocument = wordApp.Documents.Add(Visible:true);
       //Type text in our Word document
       wordApp.Selection.TypeText("This is my dynamically created word document");

       Console.WriteLine("Done");
       Console.In.ReadLine();

   }
}