Search code examples
delphi

Property override


I have a class TChild derived from TParent. TParent has a property MyProp that is reading and setting some values in an array. Of course this property is inherited by the TChild, but I want to add few extra processing in child's property. The code below explains better what I want to do but it is not working. How can I implement it?

TParent = class...
 private
   function  getStuff(index: integer): integer; virtual;
   procedure setStuff(index: integer; value: integer); virtual;
 public
   property MyProp[index: integer] read GetStuff write SetStuff
 end;

TChild = class...
 private
   procedure setStuff(index: integer; value: integer); override;
   function  getStuff(index: integer): integer; override;
 public
   property MyProp[index: integer] read GetStuff write SetStuff
 end;

procedure TChild.setStuff(value: integer);
begin
 inherited;      //  <-- execute parent 's code and
 DoMoreStuff;    //  <-- do some extra suff
end;

function TChild.getStuff;
begin
 result:= inherited;   <---- problem was here   
end;

Solution

  • Solved. The child function implementation was wrong. Basically that code works. The solution was:

    Result := inherited getStuff(Index);
    

    The problem:
    As Rob already figured it out, the problem was in TChild.GetStuff, which tried to use bare inherited like a function. Delphi doesn't allow that. You need to specify the function name and parameters if you're going to use inherited as a function.

    I already indicated where the problem was in my original question:

     <---- problem was here