The .NET Framework 2.0 introduces a variety of features and enhancements. One of these is the addition of Partial Classes. This new Framework features enables a single class definition that can be split across multiple physical files. During the compilation process, these separate files are combined to form a single class representation during runtime. One of the most important uses of this feature is that it enables developers to create an abstraction of business logic and user interface within a single class definition. In this article we will take a look at how this feature is used within the Framework 2.0 and Visual Studio 2005.
Let’s start by creating a simple Windows form application. Within this application we can implement a partial class using the following steps.
Figure 1: The solutions explorer within Visual Studio 2005
Figure 2: Adding the code to the Class1.vb file.
Figure 3: Adding the code to the Class2.vb file.
At this point we have successfully implemented an example of a partial class for this application. Within the main form when the class is instantiated, Intellisense provides the single implementation of the class as shown in Figure 4.
Figure 4: Instantiating the partial class.
If you are familiar with Visual Studio 2005 you have noticed that the designer generates a substantial amount of code within any application. For example, if we create a Windows form project as shown in Figure 5. You notice that there is a set of generated code that is automatically hidden within the form.
Figure 5: A Windows form project within Visual Studio 2003
Although this code is available there generally isn’t any reason for developers to modify or even view it. Within Visual Studio 2005 the concept of partial classes is used to remove this code completely from the designer as shown in Figure 6.
Figure 6: Within Visual Studio 2005 form generated code is not visible
Although, the code is not viewable directly within the Visual Studio 2005 IDE it is still available. For example, to view the partial class implementation within a Windows form applications you can follow these steps.
Figure 7: Create a Windows form application.
Figure 8 Selecting the show all files button
Figure 9: Partial class for the form is revealed
Figure 10: The designer generated code within Visual Studio 2005
Implementing partial classes is actually fairly easy. It is important to keep in mind a few simple rules when implementing partial classes.
Partial classes are just one of the many new features available within the .NET Framework 2.0. In this article, we have takes quick looks at how these are used by Visual Studio 2005 to simplify the IDE.