Partial Classes
June 2nd, 2007
The .NET Framework 2.0 added the ability to split your class definition into multiple physical files. To the compiler, it doesn’t make any difference, it simply brings the parts together and treats them as one. But what can partial classes do to improve your projects and when should you use them?
Well, first of all, if you’re using VS 2005 you’re already using partial classes even if you don’t realize it. The IDE uses partial classes to hide details of the user interface generation from you in its hidden Designer modules. With this built-in example usage we see one of the major benefits of partial classes, separating areas of functionality into logical units. Having this kind of clean separation works well in the IDE and can work in your own classes too.
Another effective use of partial classes is that it allows more than one developer to work on a class at a time. In earlier versions of VB if one person had a class checked out then someone else who needed to make a concurrent change to a different part of the class had to wait. Now, as long as the logic is well defined and cleanly divided, multiple developers could work on the same overall class although it would be in different physical files. Of course, care must be taken to coordinate such changes and if a class is a sprawling mess some redesign work may be in order.
Often there is some benefit to separating the methods and properties of a large class. The properties may rarely change but the methods may require more frequent adjustments. Placing them in a separate file helps prevent accidental changes and allows the method programmer to focus better on their task by reducing scrolling and making debugging easier.
Another good candidate for a partial class would be a class that implemented several interfaces. It would probably be of value to separate each implementation into its own file, particularly if one or more of the implemented interfaces were large. This can be particularly effective in the multiple programmer scenario where one developer knows one interface while another understands the other well.
One thing to note is while you can split the class in to separate source files using Partial, all parts must use the same language. Unfortunately, you can’t blend VB.NET and C# partial classes together. Maybe this will be available in a future release of Framework.
The bottom line is that Partial Classes can be of great benefit to your project if you’re dealing with large, complex, classes.
Entry Filed under: VB.NET Tutorials
Rate This Article:










Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed