Section 1.7. Hide Designer Code with Partial Types


1.7. Hide Designer Code with Partial Types

In previous versions of C# the entire definition for a class had to be in a single file. Now, using the partial keyword, you can split your class across more than one file. This provides two significant advantages:

  • You can have different team members working on different parts of the class.

  • Visual Studio 2005 can separate the designer-generated code from your own user code.


Note: Using the partial keyword, you can split your class across more than one file.

1.7.1. How do I do that?

The easiest way to see partial types at work is to examine the previous example (AnonymousMethods). Examine the declaration of the class in Form1.cs:

partial class Form1 : Form {    public Form1( )    {       InitializeComponent( );       this.button1.Click += delegate { label1.Text = "Goodbye";  };        }        // private void button1_Click(object sender, EventArgs e)    // {    //    label1.Text = "Goodbye";    // } }

The partial keyword indicates that the code in this file does not necessarily represent the complete definition of this class. In fact, you saw earlier that the Visual Studio 2005 designer generated a second file, Form1.Designer.cs, which contains the rest of the definition:

namespace AnonymousMethods {    partial class Form1    {       /// <summary>       /// Required designer variable.       /// </summary>       private System.ComponentModel.IContainer components = null;           /// <summary>       /// Clean up any resources being used.       /// </summary>       protected override void Dispose(bool disposing)       {          if (disposing && (components != null))          {             components.Dispose( );          }          base.Dispose(disposing);       }           #region Windows Form Designer generated code       /// Designer-generated initialization code       ...       #endregion           private System.Windows.Forms.Label label1;       private System.Windows.Forms.Button button1;    } }

Together, these two files completely define the Form1 class, but you are spared dealing with the designer-generated code unless you need to work with it. This makes for simpler and cleaner development.

There is some "fine print" you need to be aware of in regard to using partial classes:

  • All partial type definitions must be modified with the partial keyword and must belong to the same namespace and the same module and assembly.

  • The partial modifier can appear only before the class, interface, and struct keywords.

  • Access modifiers (public, private, etc.) must match all the partial types of the same class.

1.7.2. What about . . .

...using partial classes in my own projects?

Microsoft suggests that partial classes can allow developers to work on different aspects of a class independently. It is still too early to see what best practices will emerge; I'm inclined to think that any class that is big enough to be divided in this way is big enough to be split into two (or more) classes. For now, the primary use of partial classes is to hide the cruft created by the designer.


Tip: Robert MacNeil reports in the PBS documentary "Do You Speak American?" that cruft is a neologism invented by surfers. However, the Online Computing Dictionary (http://www.instantweb.com/D/dictionary/index.html) reports that "This term is one of the oldest in the jargon and no one is sure of its etymology." In any case, the Online Computing Dictionary defines cruft as "an unpleasant substance...excess; superfluous junk" and "the results of shoddy construction."

1.7.3. Where can I learn more?

Developer.com provides a good article on partial types. Visit http://www.developer.com/net/net/article.php/2232061 for more information.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net