Search code examples
delphidelphi-10.2-tokyo

How to let the IDE know that I use ancestor variable?


For simplicity, I only have 2 classes TParent and TChild.

TParent = class
protected
  FValue : Integer;
end;
TChild = class(TParent)
public
  property Value : Integer read FValue;
end;

If the TChild property Value uses the TParent variable FValue which is in another unit, the IDE always creates new variable when using auto-complete, which is a problem when adding new properties or methods and may cause unwanted errors.

TChild = class(TParent)
private
  FValue: Integer;
public
  property Value : Integer read FValue;
end;

However, if TParent and TChild are in the same unit, everything works fine. Is there any way to prevent this if I don't have the ability to move both classes to the same unit? Also I don't have access to a unit containing TParent. In this case, TChild is a component derived from TCustomGrid.


Solution

  • This is just the nature of inheritance, more specifically, field visibility. The simple solution would be to introduce a property getter function with a higher visibility. For example...

    TParent = class
    protected
      FValue : Integer;
    public
      function GetValue: Integer;
    end;
    
    TChild = class(TParent)
    public
      property Value : Integer read GetValue;
    end;
    
    ...
    
    function TParent.GetValue: Integer;
    begin
      Result:= FValue;
    end;
    

    Code completion is just following these same rules - it doesn't have visibility of the parent's field, so it generates a new one.