Hi, all.

> I hesitated posting this message, lest I be seen bashing Borland
> again.  But really, some of you need to know this (if you havent been
> hit by it by now).
>
> At first glance this should work fine, just the perfect hook for
> initalising fields in a new record......
>
> type
>   TTableCmn = class(TTable)
>     procedure DoOnNewRecord; override;
>   end;
>
>   TTable1  = class(TTableCmn)
>     procedure DoOnNewRecord; override;
>   end;
>
> implementation
>
> procedure TTableCmn.DoOnNewRecord;
> begin
>   inherited; /// the normal thing to do
>   FieldByName ('CommonField').AsInteger := 1;
> end;
>
> procedure TTable1.DoOnNewRecord;
> begin
>   inherited; /// let the parent initialise common stuff
>   FieldByName ('MyField').AsText  := 'blah';
> end;
>
> But, the dratted DoOnNewRecord in TTable/TDataSet calls the forms
> OnNewRecord event.  If this is assigned, it gets executed first, but
> surely, normally I want this executed last.

Hrmm.. this would only be a problem in the first place when you want the
form's assigned OnNewRecord event handler to replace (or build on top of)
behaviour you placed in the lower level DoOnNewRecord. I assume that is the
case here.

> And no, you cant call inherited last in the overridden methods.  You
> have to redo all the code on ever overridden method.   That is, the
> prime  concept of building up behaviour neatly falls down.

Hrmm.. we code using that idiom quite often. Generally speaking, there is no
problem in calling inherited at the beginning, middle, or end of your method
so long as you don't depend on any side-effects of calling inherited in the
code you insert before it.

> It would appear that this applies to all the Onxxxxx events in the
> VCL. So beware when writing families of components.

Yes, it's a common design pattern in Delphi. But by assuming you can't call
inherited later in the piece, you are boxing yourself into a potentially
unnecessary corner.

TTFN,
  Paul.


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to