Embarcadero Delphi Interfaces Properties in interfaces


Example

Since the declaration of variables in interfaces isn't possible, the "fast" way of defining properites (property Value: TObject read FValue write FValue;) can't be used. Instead, the Getter and setter (each only if needed) have to be declared in the interface, too.

IInterface = interface(IInterface)
    ['{6C47FF48-3943-4B53-8D5D-537F4A0DEC0D}']
    procedure SetValue(const aValue: TObject);
    function  GetValue(): TObject;

    property Value: TObject read GetValue write SetValue;
end;

One thing worth noting is that the implementing class doesn't have to declare the property. The compiler would accept this code:

TImplementer = class(TInterfacedObject, IInterface)
    procedure SetValue(const aValue: TObject);
    function  GetValue(): TObject
end;

One caveat, however, is that this way the property can only be accessed through an instance of the interface, noth through the class itself. Also, adding the property to the class increases the readability.