Why do we need partial classes?
Every programmer who has worked even a little bit with visual
programming tools (Visual Studio .NET 2002 or 2003) has noticed that having
access to designer generated code is lovely and gives the programmer a sense of
freedom, but once you start the programming it makes the screen quite messy. You
have the code and can edit it but once you go back to the design view, the
designer will overwrite all your changes. Most probably that was the first
reason that they introduced partial classes. In other words Visual Studio .NET
2005 gives you the chance to put your developer generated code in a different
file from the designer generated code.
How can we create a partial class?
Create a windows forms application and right-click on the form and select
the View Code item. Here is all you can see in a VB .NET Application.
| Public Class Form1
End Class |
That's it, nothing more. So actually what ever
the code that you write as a programmer will be here. Now to see the Designer
Generated Code simply go to Solution Explorer and click on the Show All Files
icon. The Form1.vb will get a + sign in front of it and once you expand it you
will come up with a file named as Form1.Designer.vb. File name has never
been a rule in .NET programming, but this is the Visual Studio .NET 2005
convention. If you open this file then you will see the Designer generated code.
Lovely; We still have access to the designer generated code, but it is not
annoying us any more.
You can create your partial class easily as well. Here is the syntax for a
simple virtual class:
| Partial Public Class
CPartialClass
End Class |
Anything else inside the class is defined as
before like a normal class.
Important notes about partial class.
- Be careful about the Partial keyword
usage:
Only one definition as Partial class will be enough to satisfy the compiler
for the other definitions. So you can have a partial class that it doesn't
have the Partial keyword in a file and actually there is no way to identify
that it is a Partial class. Class View is a very nice tool to show you what
ever that is included in different files containing parts of a certain
class. It is a good advice to explicitly add the Partial keyword for all the
definitions of a class in all files.
- Watch out for inheritance:
If your Partial class inherits another class, this inheritance is required
just in one file. Again it can be misleading for many cases. You look for a
certain functionality and it takes you some time to find out whether this
functionality is inherited or has a definition in another file in the same
class. Again the class view helps a lot. I personally consider it as a
very good advice to explicitly bring the inheritance implementation in all
the class parts in different file. Remember that neither VB .NET nor C#
support multiple inheritance, so having one part of the class inheriting one
class and another one inheriting a different class will return you a
compiler error.
- Partial class parts must be in the same
namespace:
Don't expect too much. You cannot define part of the class in one DLL and
part of it in another DLL.
Conclusion
Although partial classes are introduced to solve problems it shouldn't
make problems for you. Improper use of Partial classes can simply cause some
problems that can lead to disasters in the application maintenance or upgrade.
Don't think that they are there so let's use it. Use then carefully and
properly. In better word, know that partial class exists and works for you. Once
you really need it, go for it.
Cheers