VCLHome

VCL Programming techniques in Delphi

Introduction

In our OOP paper is the theory of object oriented programming discussed.
This paper continuous where the OOP paper stopped, and we'll take a look how everything is done inside of Delphi.
VCL stands for  Visual Component Library. This library contains many classes.
In the VCL are the components which we use in Delphi.

Before we take a look at the VCL a few advanced OOP techniques, which are used by Delphi itself, will be discussed.

Classmethods and -data

A classmethod is, just like a normal method, declared inside the class.  It starts with the key Class.

Type
   MyClass = Class
   Class function TotalCount : Integer;

Classmethods and data are shared with the whole classe, so not by a specific instance of the class.
In other words above function is  valid for all objects of the class MyClass.

Delphi uses classmethods very much, for example : counting the object of a class.

Method-pointers

A methodpointer, which looks like a procedure type, points to a  method. (sounds fair to me)

MyProcedureType=procedure (Aantal : Integer);
MyMethodPointerType = procedure(Aantal : Integer) of object;

A field in an object could be declared as follows :

type
   MyClass = Class;
   Action : MyMethodPointerType;

The cool thing about this field is that it can be used by a  method of the same kind, which means a method with the same parameters.
Suppose in an a complete different class is the following procedure declared :

Type
   AnotherClass =Class;
    Procedure DoIt (X : Integer);

And there are the next two instances :

MyObject : MyClass;
AnotherObject : AnotherClass;

The next statement is allowed :

MyObject.Action:=AnotherObject.DoIt;

If the method Action from MyObject is called, the method DoIt from AnotherObject will be executed!
The call of the method is delegated!

Delegation is the technology on which the Delphi VCL is founded.

The event-handler of a button does exactly the same. A button has a method-pointer called OnClick.
To this method-pointer can a method of the form be pointed at. This is what Delphi does behind the screens.

type
   TnotifyEvent = procedure (Sender : TObject ) of Object; //The method- pointer

MyButton = Class;
   OnClick : TNotifyEvent;
End;

TForm1 = Class (TForm)
    Procedure OnButton1Click (Sender : TObject);
    Button1 : MyButton;
end;

In the unit of  Form1 you can write the procedure underneath :.

MyButton.OnClick := Form1.OnButton1Click;

The onclick method is delegated to the Form.
All the events shown in the object inspector are properties of the method-pointer.

Classreference

Classreferences are reference towards classes. (also this sounds fair to me!)

type
  TmyClassRef = Class of TMyClass;

TNewClass = class (TMyClass);

var
  AnClassRef : TMyClassRef;
  AnObject : TMyClass;

Begin
   AnClassRef := TMyClass;
   AnObject := TMyClass.Create;
This lines can also be written as  :

AnClassRef :=TMyClass;
AnObject := AnClassRef.Create;

The classreferences compatibility rules are the same as for classes.

AnClassRef := TNewClass;

Delphi contains standard a lot of class references, here are the most important ones :

TClass = class of TObject;
TComponentClass = class of TComponent;
TControlClass = Class of TControl;

The reference TClass can be used for all references tao any class.
Because sll classes are derived from  TObject;

Classreferences can be used as follows :
Suppose on a form the next classreference is declared :

NewControl : TControl;
ClassRef : TControlClass;

We can 'connect' to the classref every control on as follows :
ClassRef := TRadioButton; of ClassRef := TEdit; etcetera.

The creation of a control :

NewControl := ClassRef.Create(Self);

Depending where ClassRef is pointing at determines what kind of control is created.
This is how Delphi works.

Properties

Objects have properties and methods.
They can be declared public, private or  protected (see the OOP paper).
There is also a key  published.
Properties which are declared published are also available in designtime. They can be set in the Object inspector.

The methods which point towards an event of the object must also be a published method.

A property is conected to a  read- and write-method.

Through properties you get acces to a field. (public or private)

Property Period : Integer
   read FPeriod write SetPeriod;

To get acces to the property Period FPeriod (a private field ) must be read.
The method SetPeriod points to value of  FPeriod.
FPeriod can also be read by a method : read GetPeriod

Whithout  write method is a property 'read only'

Events

When you click a control (a button) then a event will be generated by the control.
A control or component is for the handling of events dependent of the owner. (mostly the form) This is the delegation technique.
The event-handler of a component is a method of the form which owns the component.

Events are just properties.

In the class is an ‘onchange event’ as followed declared :

type
  TmyKlass = Class
private
  FOnChange : TNotifyEvent;
Protected
  Procedure DoChange; Virtual;
Public
  Property OnChange : TNotifyevent
      Read FOnChange write FOnChange;
end;

procedure TMyKlass.DoChange;
begin
   If Assigned(FOnChange) then
    FOnChange(Self)
end;

The method DoChange is called when something changes in MyClass and will, when FOnChange is assigned, call the 'On Change' method.
A method which changes a value is for example :

Procedure TMyClass.SetWaarde (aTeller : Integer);
begin
   aValue :=aValue + aCounterr;
   DoChange;
end;

Tip : Look at our components page for more information.

We have seen a little bit of Delphi inside now, but our motto "Keep on practising"

We will take a look at the VCL :

The hierarchy of the Delphi VCL

Every class in Delphi is a subclass of TObject.
We can use TObject as replacement for all classtypes inside the VCL.
Event-handlers have the parameter Sender of type TObject.
Object Sender can be of any type.
All  class are derived from TObject, but they are very wide subclassed. The classes are a tree, and we use the outer most classes in our programs.

Look in the Delphi help for more information about all the classes!

I've tried to give a clear view in the complex working of the Delphi VCL, and how we can interact with that.
This paper is not complete, you must practice a lot to understand all the techniques better and, even better to control them.
Studying the Delphi  VCL source-code is good way to learn the techniques.

arrows.gif (215 bytes)Top