Jaa


Looking at Partial Classes

Looking at Partial Classes

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.

Implementing a Partial Class

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

  1. Within the Windows form project add two classes as shown in Figure 1.

Figure 2: Adding the code to the Class1.vb file.

  1. Within the Class1.vb file add the code as shown in figure 2.

Figure 3: Adding the code to the Class2.vb file.

  1. Within Class2.vb add the code as shown in Figure 3. Notice that the class is defined using the Partial keyword.

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.

Simplifying the Visual Studio IDE

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.

  1. Create a new Windows Form Application as shown in Figure 7. This will create the standard project artifacts.

Figure 8 Selecting the show all files button

  1. Select the show all files button within the Solutions Explorer as shown in Figure 8

Figure 9: Partial class for the form is revealed

  1. Once all files are shown, the partial class is shown within the Solutions Explorer as seen in Figure 9.

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.

  • Any file that is part of the partial class must be in the same namespace.
  • Partial classes must use the same access modifier.
  • Every part of a partial class must be available at compile time
  • If any part of the class declares a base type, then the whole class inherits that base type.
  • If any part of the class defines an interface the entire class implements the interface.
  • Each part of the class is able to declare variables that are available to the entire class.

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.

Comments

  • Anonymous
    October 16, 2005
    Nice artical! There were some typo's though...
    For figure 1 you don't have any code in the subtractnumber() function. and you also mention Visual Studio 2004? did you mean Visual Studio 2003?
  • Anonymous
    October 16, 2005
    Thanks - looks like I had a copy edit that I have changed. Also, for the various functions it's meant to be more of an illustration.
  • Anonymous
    October 19, 2005
    Great one!!!

    All the best for your next comming articles.
  • Anonymous
    November 05, 2005
    You can have partial structs and partial interfaces too.