Properties, Methods and Events

When the team that created what would inspire Free Pascal first sat down to design a new programming language they wanted a language capable of creating visual applications foremost. They asked themselves a question. What would be the simplest manner for a programmer to interact with a button on their screen.
They proposed a simple piece of code:
Button.Caption := 'Hello World';


This idea represents the the most natural expression a programmer might use to alter the caption of button, then see that change reflected instantly on their screen. This change would occur by simply assigning a value to an object. But what needs happen to make this kind of interaction with an object and your computer screen automatic?
As it turns out
Caption
would need to detect changes to itself it and invoke code to update the screen when appropriate. The key addition to the language that supports this kind of functionality is properties.
Properties can be defined by an object with accessor methods. These accessor methods do the reading and writing of an object’s values, allowing it to examine property reads and writes before they happen. In our case, the button class would define the following property:
property Caption: string read GetCaption write SetCaption;


When assigned 'Hello World' to the caption of our button, the a property causes
SetCaption
with a value of 'Hello World' to be invoked, all behind the scene. From the programmer perspective caption just looks like a field on an object, but in reality it can invoke code, and that is the beauty of properties.
Then the language design team came up with another piece of code:
Button.OnClick := SayHello;


Now consider the line above, what is it about
OnClick
that allows it to refer to the code block named
SayHello
? And does
SayHello
in turn know where it on the screen or which button doing the clicking?
The language team would need type to not only hold a reference to
SayHello
, but also the instance to which
SayHello
belongs. This might sound confusing, but it’s actually not. Take a lok at the follow code
procedure THelloWindow.SayHello(Sender: TObject);
var
  S: string;
begin
  S := Format('Hello! This window is located at position %d, %d', [Left, Top]);
  ShowMessage(S);
end;

Now imagine you've created five duplicate
THelloWindow
instances. How does
SayHello
know if it belongs to the first window instance, the second, or even the third, and thus display the correct
Left
and
Top
coordinates? It is because the type of
OnClick
is an event, and event types will implicitly captures and holds a reference to the instance defining the
SayHello
as it is assigned. This key concept is called events, and they are the glue allowing programmers to extend an object in Free Pascal without the need to use subclassing.
You might be surprised to learn that not all languages support events. The Java language team considered the event system in Delphi when they first set out to design Java, and they ultimately settled on inner classes instead. This decision has been a thorn in their side ever since and the world has decided, the event model is superior to inner classes.