1. Computing & Technology

Discuss in my forum

A warning on dynamically instantiating components

By , About.com Guide

9 of 10

Possible Conflict with Linking Components

Notification is used for a number of purposes. One of these uses is auto-hookup. Auto-hookup occurs when a component that needs to link to another component overrides the Notification method, looking for a component of that type. When it's found (assuming the linked property is not already set), it connects automatically. A number of third-party component packages have these. Here's a variation of auto-hookup from the VCL (taken from DBTables.pas):

procedure TSession.Notification(AComponent: TComponent; Operation: TOperation) ;
begin
   inherited Notification(AComponent, Operation) ;
   if AutoSessionName and (Operation = opInsert) then
     if AComponent is TDBDataSet then
       TDBDataSet(AComponent).FSessionName := Self.SessionName
     else if AComponent is TDatabase then
       TDatabase(AComponent).FSession := Self;
end;

When "Self" is the owner, Delphi will pass your dynamically created component to every other component on the form, unnecessarily exposing your component to unexpected third-party auto-hookup code. This code can change your component's properties and assign handlers to it's events.

©2012 About.com. All rights reserved.

A part of The New York Times Company.