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

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.

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. Run time VCL creation
  6. A warning on dynamically instantiating Delphi components

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

All rights reserved.