IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceOOP Details  Print This     

Partial Class (Cross Ref > OOP Details)

Partial Class

A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler. The compiler puts the class together at compile time and treats the final class or type as a single entity exactly the same as if all the source code was in a single location.

Languages Focus

For languages that have implemented partial classes, you need to know usage details and restrictions. Can you split a class into two or more files? Can you split a class within a source code file into two or more locations? What are the details of inheritance? Does it apply to interfaces as well?

Access VBA:  "Partial Classes" Not Supported

ASP Classic:  "Partial Classes" Not Supported

C#:  "Partial Classes" partial

C# uses the keyword partial to specify a partial class. All parts must be in the same namespace.

Syntax Example:
class partial Cyborg: System.Object
{
}

Partial classes and C#

Microsoft added the concept of partial classes to C# 2.0 (along with the arrival of the .Net Framework 2). With the .Net implementation, you can split a class into multiple source files, and/or multiple locations in the same source file so long as they are all in the same namespace. All parts must use the same base class so it's typical to indicate the base class in only the first part. All other parts don't need to specify the base class. All the parts included at compile time are compiled. This presents some interesting possibilities.

.Net Features

  • Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations.
  • Any class, struct, or interface members declared in a partial definition are available to all of the other parts.

.Net Limitations

  • All parts must be defined within the same namespace.
  • All the parts must use the partial keyword.
  • All of the parts must be available at compile time to form the final type.
  • All the parts must have the same accessibility (i.e. public, private, etc.).
  • If any of the parts are declared abstract, then the entire type is abstract.
  • If any of the parts are declared sealed, then the entire type is sealed.
  • If any of the parts declare a base type, then all parts use that same base type. All parts that specify a base class must specify the same base class. You can omit a base class in one or more of the parts in which case the part still uses the same base class.

Visual C# 2008 Working Winforms Example

The following simple example demonstrates partial classes.

Create a form and place a button on it and add code as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace Partial
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
  
    private void button1_Click(object sender, EventArgs e)
    {
      Cyborg MyRobot = new Cyborg();
     
      //Optionally set CyborgName property:
      //MyRobot.CyborgName = "Cameron";
  
      MyRobot.IntroduceYourself();
    }
  }
  
  //
  //One use of partial classes is to organize
  //large classes into logical groups. For example,
  //you can use partial classes to separate your
  //class properties and methods. Although these
  //are in the same file, you could put them in
  //separate source files.
  //
  //Cyborg Properties...
  public partial class Cyborg : System.Object
  {
    public string CyborgName { get; set; }   
  }
  
  //Cyborg Methods...
  public partial class Cyborg : System.Object
  {
    public virtual void IntroduceYourself()
    {
      if (CyborgName == null)
        MessageBox.Show("Hi, I do not have a name yet.");
      else
        MessageBox.Show("Hi, my name " + CyborgName + ".");
    }
  }

}
 

 

 



Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Advanced

3 Advanced Level Questions

Question #1: Multiple Choice

You can put parts of a partial class where?

Answer:
1. 

In the same source file, namespace does not apply.

2. 

In the same source file, but all parts must be in the same namespace.

3. 

Anywhere within a namespace even in separate source files.

4. 

Each part must be in a separate namespace and in a separate source file.

Question #2: Yes or No?

Do all parts of a partial class have to be in the same source file?

Answer:
  • Yes
  • No
  • Question #3: True or False?

    All parts of a partial class must be in the same namespace.

    Answer:
  • True
  • False
  • More Info

    Code:  C# Partial Classes (partial)
    Definition:  Partial Class

    C++:  "Partial Classes" Not Supported

    In C++, you can split the implementation of a class among two or more source files. However, you must declare the member in the class declaration.

    C++/CLI:  "Partial Classes" Not Supported

    C++/CLI does not yet support partial classes. My assumption is that it will soon because it is a .Net language�but only time will tell.

    Corel Paradox:  "Partial Classes" Not Supported

    Delphi:  "Partial Classes" Not Supported

    As of Delphi 2009, partial classes are not supported. The main reason given in the thread below was that the Delphi compiler is a single pass compiler. Here is a link to a discussion thread on the subject:

    Delphi Prism:  "Partial Classes" partial

    Prism supports both partial classes and partial methods using the keyword partial. A partial method is an empty method defined in a partial class.

    Syntax Example:
    //Organize a large class in multiple files.
    T800 = partial class(Cyborg, IHuman);
    end;
      
    T800 = partial class(ITalk);
    end;
      
    //Partial methods too:�
    T800 = public partial class
    private
    method Walk; partial; empty;
    method Run; partial; empty;
    end;

    Partial classes and Prism

    With Prism, you can split a class into multiple source files, and/or multiple locations in the same source file so long as they are all in the same namespace. All parts must use the same base class so it's typical to indicate the base class in only the first part. All other parts don't need to specify the base class. All the parts included at compile time are compiled. This presents some interesting possibilities.

    .Net Features

    • Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations.
    • Any class, struct, or interface members declared in a partial definition are available to all of the other parts.

    .Net Limitations

    • C++/CLI does not yet support partial classes.
    • All parts must be defined within the same namespace.
    • All the parts must use the partial keyword.
    • All of the parts must be available at compile time to form the final type.
    • All the parts must have the same accessibility (i.e. public, private, etc.).
    • If any of the parts are declared abstract, then the entire type is abstract.
    • If any of the parts are declared sealed, then the entire type is sealed.
    • If any of the parts declare a base type, then all parts use that same base type. All parts that specify a base class must specify the same base class. You can omit a base class in one or more of the parts in which case the part still uses the same base class.

    Prism Working Winforms Example

    The following simple example demonstrates partial classes. Although partial classes have many uses, in this demonstration I am splitting the properties from the methods (a whopping one of each). In this demo, all code is in this one file along with the form. However, in production you could put the methods and properties in their own source files as in CyborgProperties.pas and CyborgMethods.pas or divide the code in any other way you wish. This is particularly convenient with large classes in a multi-developer environment where the team is using a version control system.

    Create a form and place a button on it and alter code as follows:

    namespace CR_Partial;
      
    interface
      
    uses
      System.Drawing,
      System.Collections,
      System.Collections.Generic,
      System.Linq,
      System.Windows.Forms,
      System.ComponentModel;
      
    type
      /// <summary>
      /// Summary description for MainForm.
      /// </summary>
      MainForm = partial class(System.Windows.Forms.Form)
      private
        method button1_Click(sender: System.Object; e: System.EventArgs);
      protected
        method Dispose(disposing: Boolean); override;
      public
        constructor;
      end;
      //
      //One use of partial classes is to organize 
      //large classes into logical groups. For example, 
      //you can use partial classes to separate your 
      //class properties and methods. Although these 
      //are in the same file, you could put them in 
      //separate source files. 
      //
      //Cyborg Properties...

      Cyborg = public partial class(System.Object)
      public
        property CyborgName: String;
      end;
      
      //Cyborg Methods...
      Cyborg = public partial class(System.Object)
      public
        method IntroduceYourself;
      end;
      
    implementation
      
    {$REGION Construction and Disposition}
    constructor MainForm;
    begin
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    end;
      
    method MainForm.Dispose(disposing: Boolean);
    begin
      if disposing then begin
        if assigned(components) then
          components.Dispose();
        //
        // TODO: Add custom disposition code here
        //
      end;
      inherited Dispose(disposing);
    end;
    {$ENDREGION}
      
    method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
    begin
      var MyRobot := New Cyborg;
      
      //For demo, optionally set CyborgName property:
      //MyRobot.CyborgName := "Cameron";
      
      MyRobot.IntroduceYourself;
    end;
      
    method Cyborg.IntroduceYourself;
    begin
      If Length(CyborgName) > 0 Then
        MessageBox.Show("Hi, my name is " + Self.CyborgName)
      Else
        MessageBox.Show("Hi, I do not have a name yet.");
    end;
    end.

    More Info

    Code:  Delphi Prism Partial Classes (partial)
    Definition:  Partial Class

    Java:  "Partial Classes" Not Supported

    VB Classic:  "Partial Classes" Not Supported

    VB.Net:  "Partial Classes" Partial

    VB.Net supports both partial classes and partial methods.

    Syntax Example:
    Partial Public Class Cyborg
    Inherits System.Object
    End Class

    Partial classes and VB.Net

    Microsoft added the concept of partial classes to VB.Net 2005 (along with the arrival of the .Net Framework 2). With the .Net implementation, you can split a class into multiple source files, and/or multiple locations in the same source file so long as they are all in the same namespace. All parts must use the same base class so it's typical to indicate the base class in only the first part. All other parts don't need to specify the base class. All the parts included at compile time are compiled. This presents some interesting possibilities.

    .Net Features

    • Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations.
    • Any class, struct, or interface members declared in a partial definition are available to all of the other parts.

    .Net Limitations

    • All parts must be defined within the same namespace.
    • All the parts must use the partial keyword.
    • All of the parts must be available at compile time to form the final type.
    • All the parts must have the same accessibility (i.e. public, private, etc.).
    • If any of the parts are declared abstract, then the entire type is abstract.
    • If any of the parts are declared sealed, then the entire type is sealed.
    • If any of the parts declare a base type, then all parts use that same base type. All parts that specify a base class must specify the same base class. You can omit a base class in one or more of the parts in which case the part still uses the same base class.

    VB.Net 2008 Working Winforms Example

    The following simple example demonstrates partial classes. Although partial classes have many uses, in this demonstration I am splitting the properties from the methods (only one of each). In this demo, all code is in this one file along with the form. However, in a real coding situation you could put the methods and properties in their own source files as in CyborgProperties.vb and CyborgMethods.vb or divide the code in any other way you wish. This is particularly convenient with large classes in a multi-developer environment where the team is using a version control system.

    Create a form and place a button on it and alter code as follows:

    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyRobot As Cyborg
        MyRobot = New Cyborg
        MyRobot.CyborgName = "Cameron"
        MyRobot.IntroduceYourself()
      End Sub
    End Class
      
    Partial Public Class Cyborg
      Inherits System.Object
      Private FCyborgName As String
      Public Property CyborgName()
        Get
          Return FCyborgName
        End Get
        Set(ByVal value)
          FCyborgName = value
        End Set
      End Property
    End Class
      
    Partial Public Class Cyborg
      Public Sub IntroduceYourself()
        If CyborgName.ToString.Length > 0 Then
          MessageBox.Show("Hi, my name is " & CyborgName & ".")
        Else
          MessageBox.Show("Hi, I do not have a name yet.")
        End If
      End Sub
    End Class

    More Info

    Definition:  Partial Class
    Code:  VB.Net Partial Classes (Partial)




    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2024 Prestwood IT Solutions.   [Security & Privacy]