Subject delphi7: How to inherited and set a method in TIB_Query
Author



I am trying to create a component descended from TIB_Query so that I can attach an OnNewRecord method handler which I almost always do manually for each TIB_Query that I drop on my project, like this one:

procedure DM1.IB_Query1OnNewRecord(Sender : TIB_Dataset);
begin
  IB_Query1.FieldByName('PRODUCT_ID').asinteger := -1;
end;

And this is my attempt to subclass TIB_Query:

    type
        TMyIB_Query = class(TIB_Query)
        private
            FMyGeneratorName: string;
            FMyIDField: string;
            FUserOnNewRecord: TIB_DatasetEvent;
            procedure MyNewRecord(Sender: TIB_Dataset);
        public
            constructor Create(AOwner: TComponent); override;
            procedure GeneratePK;
            procedure GenerateTempPK;
        published
            property MyGeneratorName: string read FMyGeneratorName write FMyGeneratorName;
            property MyIDField: string read FMyIDField write FMyIDField;
        end;

    constructor TMyIB_Query.Create(AOwner: TComponent);
    begin
        inherited Create(AOwner);
        if not (csDesigning in ComponentState) then begin
            if Assigned(OnNewRecord) then  // <--- OnNewRecord is always nil
                FUserOnNewRecord := OnNewRecord;
            OnNewRecord := MyNewRecord;
        end;
    end;   

    procedure TMyIB_Query.GeneratePK;
    begin
        if FieldByName(MyIDField).AsInteger < 0 then
            FieldByName(MyIDField).AsInteger := Gen_ID(MyGeneratorName,1);
    end;

    procedure TMyIB_Query.GenerateTempPK;
    begin
        FieldByName(MyIDField).AsInteger := -1;
    end;

    procedure TMyIB_Query.MyNewRecord(Sender: TIB_Dataset);
    begin
        if Assigned(FUserOnNewRecord) then
            FUserOnNewRecord(Sender);
    end;

Usage: The inherited TMyIB_Query is supposed to assign a temporary PK of -1 for every insert.

Problem: If no OnNewRecord method is assigned to a dropped TMyIB_Query, the default method fires. However if a method is assigned to the dropped TMyIB_Query, only the assigned method fires, but not the default. Tracing in debug, it appears that OnNewRecord is always _nil_.

Please, can you tell me what did I do wrong?

(PS: This implementation works OK with TClientDataSet.)