Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

The Sender Parameter and Substitution - Delphi OOP Part 5 - Chapter 11

By Zarko Gajic, About.com

OOP Chapter 11 - Sender
Materials written by John Barrow. Modifications by Zarko Gajic

Back to Chapter 10

Introducing (in this Chapter):

  • Substitution
  • The Sender parameter
  • Typecasting
  • Class and instance operators
  • RTTI: run time type information

Introduction

An important consequence of an inheritances hierarchy is the concept of substitution, where an instance of a subclass object can substitute for a variable declared as any of its supertypes. In this brief chapter we introduce the concept of substitution by investigating the Sender parameter that is part of the parameter list of every event handler.

Different events have different parameter lists but the first in the list for all events is Sender: TObject, which identifies the component that initiated the event. Identifying the event initiator is a useful idea, but not completely straightforward. The difficulty is that every parameter must have a type, and so Sender must be declared as a specific type. But what type should Sender be? Every component is different. As we’ll see in the example that follows, Delphi uses the inheritance hierarchy to solve this problem.

Note: for a start you can read the "Understanding the Sender parameter in Delphi Event Handlers"

Example 5.1 The Sender parameter in event handlers

The example we’ll use to illustrate the Sender parameter has six SpeedButtons, two panels, a label and a form. The goal of this program is to display an appropriate message in response to a click on any of these components. One way of doing this is to write ten event handlers, one for each component, each displaying the required message.

But this leads to a lot of effort and, worse, the repeated code introduces many opportunities for future errors. What would be better would be to write just one procedure, to route all the OnClick events through this code, and then use the Sender parameter to generate a message identifying which component has been clicked.

Ex 5.1 step 1 The OnClick event handler, version 1

We’ll start with some simple code and then develop it into the complete program. Create an OnClick event handler for spdOne to display the value of spdOne’s Caption and then test this first version of the program.
28 procedure TfrmSender.spdOneClick(Sender: TObject) ;
29 begin
30    pnlReport.Caption := 'You clicked on ' + spdOne.Name;
31 end;

Ex 5.1 step 2 Using the Sender parameter

Now we’ll modify this event handler to use the Sender parameter. One possibility is to change spdOne in the assignment statement to Sender (line 30 below):
28 procedure TfrmSender.spdOneClick(Sender: TObject) ;
29 begin
30    pnlReport.Caption := 'You clicked on ' + Sender.Name;
31 end;
If you try this you’ll see that it does not work. The compiler returns an ‘Undeclared identifier’ error. Why is this? In the parameter list in the header of this OnClick event handler, and all other event handlers, Sender’s type is defined as TObject, the root class of the hierarchy, and not as TSpeedButton (line 28). The TObject class does not have a Name property, and so, when the compiler reaches Sender.Name in line 30, it generates an error.

Because Sender is a completely general parameter it is declared as type TObject. However, in this case we know that Sender is a TSpeedButton and so we must instruct Delphi to treat Sender as a TSpeedButton – in other words we must typecast Sender as a TSpeedButton. There are different ways to do this. Here we use the ‘as’ class operator as follows (line 31):

28 procedure TfrmSender.spdOneClick(Sender: TObject) ;
29 begin
30    pnlReport.Caption := 'You clicked on ' + (Sender as TSpeedButton).Name;
31 end;
This program compiles and works like the previous version, so we now seem to be using the Sender parameter correctly. We have seen the "as" typecasting operator in passing in example 2.1 step 2 line 17, and here we will look at it more closely. This typecasting informs Delphi that although Sender is declared in the parameter list as a TObject because it could be any one of a number of different classes, in this particular case Delphi must treat it as a TSpeedButton (line 31 above).

Ex 5.1 step 3 Linking in the other SpeedButtons

None of the other Buttons display a message yet and so we must now pay attention to linking them in. Close the program, select spdOne and then select the Events Tab in the Object Inspector. To make things less confusing, change the name of the OnClick handler from spdOneClick to GeneralClick. Delphi will now automatically make this change in the program code. (Don’t change the program directly.) Now link the other SpeedButtons to this event handler. Selecting each SpeedButton in turn, on the Events Tab of the Object Inspector select ‘GeneralClick’ from the drop-down box alongside OnClick. This routes each SpeedButton’s event to the single ‘General Click’ event handler. Save this version of the program and run it.

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. The Sender Parameter and Substitution - Delphi OOP Part 5 - Chapter 11

©2009 About.com, a part of The New York Times Company.

All rights reserved.