Office and COM interop with .NET: *Now* I'll give you a second glance

by Jon Davis 10. July 2009 14:27

Well we're not quite a decade yet--certainly a lot longer than five years, though--since .NET came out and promised to be the next big thing for programming. C# was supposed to put VB6 to shame. It didn't, though, because with VB6 you had such things as optional parameters and variants--the latter being an ugly beast but under the covers the flexibility that VB6 was made programming so much quicker and easier sometimes.

Now that C# 4.0 introduces optional parameters and dynamic objects, the whole playing field is changing. And now I can feel a bit freer to tinker with old-school technologies built on COM in my language of choice because the language has finally caught up with last decade's featureset in this respect.

I like this somewhat recent blog post by SamNG:

http://blogs.msdn.com/samng/archive/2009/06/16/com-interop-in-c-4-0.aspx

Lets look at a quick Office example.

static void Main()
{
    Word.Application app = new Word.Application();
    object m = Type.Missing;
    app.Documents.Add(ref m, ref m, ref m, ref m);
}

This is such typical code! I have to struggle with the type system to make it happy, just to add a simple Word document!

... [With C# 4.0] in the following code, call (1) gets transformed into call (2).

static void Main()
{
    Word.Application app = new Word.Application();
    // (1) Your initial call.
    app.Documents.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    // (2) Compiler generates locals and inserts them.
    object m = Type.Missing;
    app.Documents.Add(ref m, ref m, ref m, ref m);
}

The great thing about this too, is that with the introduction of named and optional arguments, and using the fact that the feature generates Type.Missing in place of default values for object on COM types, we can simply remove the arguments altogether!

static void Main()
{
    Word.Application app = new Word.Application();
    // (1) Your optional-parameter-omitted initial call.
    app.Documents.Add();

    // (2) Compiler generates locals and inserts them.
    object m = Type.Missing;
    app.Documents.Add(ref m, ref m, ref m, ref m);
}

Pretty cool stuff huh? Definitely makes programming against the Office APIs much nicer.

Yup. Indeed.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Software Development

Silverlight 3: 3D support has buggy occlusion?

by Jon Davis 10. July 2009 14:07

I just downloaded Silverlight 3 and Blend 3. First thing I did was try out one of the samples. I grabbed the Zune 3D sample, then ran F5 to run it. I was able to spin the Zune model by dragging the mouse from the base of the model. But then I immediately noticed that it doesn't look right. The arrows and artwork that are on the sides of the model do not get properly occluded while the whole thing is spun. The artwork occludes too soon, and the arrows don't occlude at all.

Could be a bug in the sample. But could be a glitch in Silverlight itself. I wonder?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Silverlight


 

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Jon Davis Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
 
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up." 

Amazon Collection

Most Recent of Many Library Investments

Tag cloud

Calendar

<<  July 2009  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar