Upload
felicity-simmons
View
213
Download
0
Embed Size (px)
Citation preview
Delphi Classes – roll your own
Delphi classes Modify to change visibility of properties
Inherit from others Add extra specialised behaviour
Create your own Create completely new classes
Customer Preferences Connection
Delphi Classes - scope Delphi classes
Private Visible within the class
Protected Visible to ‘children’ of the class
Public Visible to all users of the class
Published Visible properties to all users of the class
Delphi Classes – modify Classes have certain properties made
visible Every class has a custom version –
with no properties visible Use this to decide which fields or
attributes you wish to promote to published properties
Can only make more visible in sub-classes not more private
Inherit from others Inherit from the custom version
TCustomListBox TCustomComboBox TCustomADODataSet etc..
Add extra fields or methods Decide which fields are to be
published Still have access to all the built in
behaviour
Create your own classes Create completely new classes
Descend from appropriate base class Control Graphic List etc.,
Or from the most basic - TObject
Create your own – Syntax 1 Help, Object Pascal Language Guide
Available from the Delphi Menu Or via the ‘Manual’ web page
Help, Main Delphi Help Class completion Tells you how to define a basic class
Create your own – Syntax 2unit helperObjects;
interface
uses classes, graphics;
type TMyPrefs = class(TObject) property Name: String; property Color: TColor;end;
Press Ctrl+Shift+C for Class Completion
And you get:
Create your own – Syntax 2unit helperObjects;
interface
uses classes, graphics;
type TMyPrefs = class(TObject) private FColor: TColor; FName: TStrings; procedure SetColor(const Value: TColor); procedure SetName(const Value: String); published property Name: String read FName write SetName; property Color: TColor read FColor write SetColor;end;. . .
Create your own – Syntax 3. . .implementation
procedure TMyPrefs.SetColor(const Value: TColor);Begin FColor := value;End;
procedure TMyPrefs.SetName(const Value: String);Begin if Fname < > Value then FName := Value; end;
Using a class you’ve created 1 In the unit which will use your class
you must: Add the name of your unit
(helperObjects in my example) to the uses clause in the interface section
Declare a variable of type TMyPrefs Private
preferences: TMyPrefs; Create an object of type TMyPrefs
Using a class you’ve created 2 In the unit which will use your class
you must: Create an object of type TMyPrefs
In form OnCreate add: Preferences.create;
Destroy the object before form closes In form OnDestroy add: Preferences.destroy;
Using a class you’ve created 3 In the unit which will use your class,
to use it you’ll need: To write to the property:
Preferences.Name := form.font.name; Preferences.Color := form.color;
To read from the property: Label1.Caption := Preferences.Name;
Using a class with Set / Get Properties with set get functions
property Name: String read FName write SetName
encapsulation We can assign to Name but actually call
SetName Allows us to check value ranges Change values without the user seeing Hides complexity.