E2174 '%s' not previously declared as a PROPERTY (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Error and Warning Messages (Object Pascal) Index

You have attempted to hoist a property to a different visibility level by redeclaration, but <name> in the base class was not declared as a property. -W


program Produce;
(*$WARNINGS ON*)

  type
    Base = class
    protected
      Caption : String;
      Title : String;
      property TitleProp : string read Title write Title;
    end;

    Derived = class (Base)
    public
      property Title read Caption write Caption;
    end;

begin
end.

The intent of the redeclaration of 'Derived.Title' is to change the field which is used to read and write the property 'Title' as well as hoist it to 'public' visibility. Unfortunately, the programmer really meant to use 'TitleProp', not 'Title'.


program Solve;
(*$WARNINGS ON*)

  type
    Base = class
    protected
      Caption : String;
      Title : String;
      property TitleProp : string read Title write Title;
    end;

    Derived = class (Base)
    public
      property TitleProp read Caption write Caption;
      property Title : string read Caption write Caption;
    end;

begin
end.

There are a couple ways of approaching this error. The first, and probably the most commonly taken, is to specify the real property which is to be redeclared. The second, which can be seen in the redeclaration of 'Title' addresses the problem by explicitly creating a new property, with the same name as a field in the base class. This new property will hide the base field, which will no longer be accessible without a typecast. (Note: If you have warnings turned on, the redeclaration of 'Title' will issue a warning notifying you that the redeclaration will hide the base class' member.)