Partial Classes and the Windows Forms Designer
In Whidbey we'll be getting the new concept or Partial Classes.
For those who don't know, the concept is fairly simple - you can declare code for a class over more than one file. I can think of times when this is a handy feature, but I think this will be abused more than it's actually useful. Here's a quick example:
Public Class MyPartial
Public Sub New()
msSomething = "Foo"
End Sub
End Class
Public Partial Class MyPartial
Private msSomething As String
End Class
As far as both the IDE and the compiler are concerned, these classes are one and the same, all smooshed together :)
Now here's where it gets tricky. I'm using VB.net here, and Beta 1, so things may have changed, I don't know. When you add a form to a project, the VS team have decided that you really don't want to see all that 'Windows Forms Designer generated code' gunk, and split it out into a partial class file, and that file is not automatically part of your project.
As a result, when I add go to see the code behind my brand new 'Form1', all I see is this:
Public Class Form1
End Class
Now, this is actually pretty cool. But what do I do about member initialization? If i have a member variable, I want to initialize it to something predictable, normally in Sub New() - I've become a firm believer in leaving the Form_Load() event completely alone, which we needed in the VB6 days.
So I go and make a Sub New and do my work where I want to - as any innocent programmer would do, surely :) - and get a lovely error telling me that 'Public Sub New() has multiple definitions with identical signatures'.
I'm pretty forgiving with the whole 'VB programmers are stupid' line - I know that I'm special and actually have a clue, unlike a lot of people (hobbyist/newbie programmers or not *grin*) but this is starting to push me a little too far. With all the tenets of how .net gives us OOP, etc, surely even the most gumby programmers should be expected to be able to handle seeing the constructor?
I checked with a C# project, and they've done a little better. They still use partial classes to hide the generated code, but the constructor is inserted in main code, where the programmer can see it straight away. It contains a call to InializeComponent, which you can track down and view easily if you want to, although they neglected to leave a comment in saying 'hands off!'.
*sigh*. Someone please tell me the constructor has been moved out to the main code file in later drops!
Listening to: underneath it all - no doubt - (5:04)