Welcome to CrankyGoblin.Com Sign in | Join | Help

Public Class GeoffAppleby

Inherits Microsoft.VisualBasic.MVP : Implements IBrainFart
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)
Posted: Monday, 14 February 2005 12:48 PM by Geoff Appleby
Filed under:

Comments

Ranjan said:

IMHO, the placement of a default constructor in the generated section is stupid. Ive tried partial classes in C#, its good that somebody actually thought before placing the constructor in the main part of the whole.
Maybe because most VB guys use Form_Load ? but still, thats not an excuse at all. maybe someone can enlighten us.
# February 14, 2005 3:59 PM

Daniel Moth said:

The November CTP (which is the latest public release from a version perspective) still does the "stupid" thing. However (and I wish I could find the reference) I remember reading that by RTM this will change to reflect the existing C# way :-)

On a separate note, I also think it is daft that in VB (unlike C#) you can omit the "Partial" from one of the class declarations (with C# looking at any class immediately tells you if you are looking at a partial (no pun intended) view of the class).

Finally, you may find useful my summary of how Partial classes work from an IDE perspective:
http://www.danielmoth.com/Blog/2004/09/partial-classes-cf-full-fx.html
# February 14, 2005 5:51 PM

Geoff Appleby said:

Thanks dude, I was hoping someone would know if it's been changed :)

I agree that while in VB it's nice that one occurence of the word 'partial' can be left out, hopefully it's not a requirement. I must test that :)

Time to search the product feedback centre i think.
# February 14, 2005 7:17 PM

JDubya said:

I hate partial classes, they obfuscate too much code for me.

Why is MS forcing this paradigm on me? I don't want to use them, it makes my life harder and actually increases my development time.

Is there a way to get VS.NET 2005 to not use partial classes and behave like VS.NET 2003?

Please for the love of everything good, tell me there is.
# February 8, 2006 9:37 AM

Matthew said:

>> Please for the love of everything good, tell me there is.

Cut and paste the code and delete the designer file. It isn't exactly rocket science.
# April 11, 2006 6:34 PM

Niko said:

Converting a project/solution from VS2003 to VS2005/2008 will not convert Forms and UserControls source code to the new Designer+User style (using partial classes); they are left untouched in a single-file style.

That's why I wrote DeCodEx, a simple and free tool to perform this conversion in a safe and quick way.

You could download it from here: http://coolsoft.altervista.org/DeCodEx

Please do not consider this as spam..., the tool is free!

# February 25, 2008 3:05 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

To submit your comment, click on these pictures:
  • Geoff's big sister's tongue
  • Geoff's tongue
  • Tickle Me Geoff-Mo
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Can't recognise the people in these pictures? Look here for a quick introduction.
There's a time limit for you to get your comment submitted before this set of pictures expires. If you think it's been longer than 10 minutes, get some new pictures first (you won't lose what you've typed so far).
Get some new pictures 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS