Your full service technology partner! 
-Collapse +Expand
Search Prism Group:

-Collapse +Expand Prism To/From
-Collapse +Expand Prism Study Test
-Collapse +Expand Prism Store

Prestwood eMagazine

June Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi PrismOOP   Print This     
Next Random Article Next Random Tidbit || Change Topic Random From All
  From the January 2016 Issue of Prestwood eMag
Prism OOP:
Delphi Prism Partial Classes (partial)
Posted 13 years ago on 1/6/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page


General Info: 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: Partial Class

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?

Delphi Prism Partial Classes

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);
T800 = partial class(ITalk);
//Partial methods too:�
T800 = public partial class
method Walk; partial; empty;
method Run; partial; empty;

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

More Info

Definition:  Partial Class


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1283A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101794 Counter
Since 1/6/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

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