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

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingDelphi PrismOOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Prism OOP:
Delphi Prism Partial Classes (partial)
 
Posted 15 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

KB101794

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);
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

Definition:  Partial Class

Comments

0 Comments.
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 = P133A1
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 play-drums.com 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
16252
Since 1/6/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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