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
|