E2209 Field declarations not allowed in interface type (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

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

An interface has been encountered which contains definitions of fields; this is not permitted.


program Produce;
  type
    IBaseIntf = interface
      FVar : Integer;
      property Value : Integer read FVar write FVar;
    end;

begin
end.

The desire above is to have a property which has a value associated with it. However, as interfaces can have no fields, this idea will not work.


program Solve;
    IBaseIntf = interface
      function Reader : Integer;
      procedure Writer(a : Integer);
      property Value : Integer read Reader write Writer;
    end;

begin
end.

An elegant solution to the problem described above is to declare getter and setter procedures for the property. In this situation, any class implementing the interface must provide a method which will be used to access the data of the class.