Wednesday, November 24, 2004

Partial Classes

Partial Classes are a new feature of Visual Studio 2005. Basically they allow you to spread code across multiple code files. I have heard a lot of criticism about partial classes, mainly the whole "If you need partial classes then your class is too big".. Not so.

A good use for partial classes is with typed datasets. A lot of developers I have talked to use typed datasets for all of there Business Objects. The only problem is when you modify the database, then re-generate the typed datasets, any custom code is lost. By having the additional code defined in a separate file, this problem can now be avoided.

My only complaint with partial classes actually has to do with the functionality of visual studio 2005, more than partial classes themselves. My development team has a standard in place, where developers are required to use Overrides where possible instead of events. An example of this is using OnLoad override instead Form.Load event. Now, when you double click on a form in VS.NET 2k5, VS automatically creates a Form.Load event handler. In VB.NET, all you have to do is delete the actual event handler and all is fine. C# is a different matter. Not only do you now have to delete the actual Callback method, you now have to locate the "Hidden by Default" partial class containing all the designer code and remove the declaration for the Event Handler.

I actually dropped by the VS beta site and left a suggestion to allow the user to override the action performed when double clicking, but unfortunately I was told it was too late in the development process to look at that.. Hopefully we will see a change in Orcas??

No comments: