February 13, 2005

One more great thing about Whidbey: Partial form classes

I've been toying with Whidbey quite a lot lately. One thing I didn't fully appreciate from reading the spec at first was partial classes. Sure, the concept of being able to divide a class in several files is great, but the full realization of its practical benefits kept me waiting until I designed my first complex form with VS.NET 2005.

#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace MyClient
{
  partial class MainForm : Form
  {
    public MainForm()
    {
      InitializeComponent();
    }
  }
}

This is what I get when I pick "View code" for the form. Where are the control declarations? Where's that InitializeComponent() method maintained by the form designer? That's right, they're somewhere I can't see, and that's right where they belong. In practice, they're in MainForm.Designer.cs, which is totally maintained by the designer, as opposed to my MainForm.cs, which is totally maintained by me (except for VS.NET adding event handler stubs when I request so).

If I had had this little facet of separation at my disposal for the last few years, I would've been a much happier man. Not to say the current (2003) Form designer model doesn't work, but it's certainly clumsy and error-prone. The borders of machine-generated code and human creation are always weak spots. Now we're one step further from confusing the designed form and the integrated logic. An excellent innovation, that.

PS. I've done quite a lot recently, even though you can't tell by reading the blog. "Test-Driven Development in Microsoft .NET" is among the titles I've recently read - and heartily recommend. More on that soon, I hope.

Posted by Jouni Heikniemi at February 13, 2005 08:14 AM
.net
Comments

Now if only there was a way to convert pre-Whidbey forms to use partial classes, I would be an even happier man.

Perhaps a perl hack is in order...

Posted by: Dave at February 14, 2005 04:25 AM
Post a comment









Remember personal info?