2.8. Split a Class into Multiple Files
If
you've cracked open a .NET 2.0 Windows Forms class,
you'll have noticed that all the automatically
generated code is missing! To understand where it's
gone, you need to learn about a new feature called partial
classes, which allow you to split classes into several
pieces.
Note: Have your classes grown too large to manage in one file?
With the new Partial keyword, you can split a class into separate
files.
2.8.1. How do I do that?
Using the new Partial keyword, you
can split a single class into as many pieces as you want. You simply
define the same class in more than one place. Here's
an example that defines a class named SampleClass
in two pieces:
Partial Public Class SampleClassIn this example, the two declarations are in the same file, one after
Public Sub MethodA( )
Console.WriteLine("Method A called.")
End Sub
End Class
Partial Public Class SampleClass
Public Sub MethodB( )
Console.WriteLine("Method B called.")
End Sub
End Class
the other. However, there's no reason that you
can't put the two SampleClass
pieces in different source code files in the same project. (The only
restrictions are that you can't define the two
pieces in separate assemblies or in separate namespaces.)When you build the application containing the previous code, Visual
Studio will track down each piece of SampleClass
and assemble it into a complete, compiled class with two methods,
MethodA( ) and MethodB( ). You
can use both methods, as shown here:
Dim Obj As New SampleClass( )Partial classes don't offer you much help in solving
Obj.MethodA( )
Obj.MethodB( )
programming problems, but they can be useful in breaking up extremely
large, unwieldy classes. Of course, the existence of large classes in
your application could be a sign that you haven't
properly factored your problem, in which case you should really break
your class down into separate, not partial, classes. One of the key
roles of partial classes in .NET is to hide the designer code that is
automatically generated by Visual Studio, whose visibility in
previous versions has been a source of annoyance to some VB
programmers.For example, when you build a .NET Windows form in Visual Basic 2005,
your event handling code is placed in the
source code file for the form, but the designer code that creates and
configures each control and connects its event handlers is nowhere to
be seen. In order to see this code, you need to select Project
Visual Studio menu. When you do, the file that contains the missing
half of the class appears in the Solution Explorer as a separate
file. Given a form named Form1, you'll actually wind
up with a Form1.vb file that contains your code
and a Form1.Designer.vb file that contains the
automatically generated part.
2.8.2. What about...
...using the
Partial keyword with structures? That works, but
you can't create partial interfaces, enumerations,
or any other .NET programming construct.
2.8.3. Where can I learn more?
To get more details on partial classes, refer to the index entry
"Partial keyword" in the MSDN Help.