Search code examples
delphioopinterfacedelphi-7

Interfaces and property visibility


I have a simple interface

  ISomeProperties = interface
  ['{3AD52E4E-5190-4ABE-8AFC-062295E3A352}']
    function GetPort: integer;
    procedure SetPort(const Port: integer);
  end;

GetFunction and SetFunction is visible in code complection. But after I add a property like this

  ISomeProperties = interface
  ['{3AD52E4E-5190-4ABE-8AFC-062295E3A352}']
    function GetPort: integer;
    procedure SetPort(const Port: integer);
    property Port: integer read GetPort write SetPort;
  end;

GetPort and SetPort methods disappear only propert Port is visible - great.

Now I implement an interface

  TSomeProperties = class(TInterfacedObject, ISomeProperties)
  private
    function GetPort: integer;
    procedure SetPort(const Port: integer);
  end;

But the property Port is not visible from a class that implements the interface! Is that a desired behaviour or I'm doing something wrong?


Solution

  • The class is not the interface. Properties on interfaces are mere syntactic sugar, exposing the methods GetPort and SetPort for Delphi.

    This property is not really something that must or can be implemented (only the accessor methods), so it is not visible in the implementing class, unless you define a property there too. The only things you can implement are methods.

    FWIW, the methods do not "disappear" when you define the property. You can still call them. All members of an interface have the same visibility.