Fabrizio
Fabrizio

Reputation: 8043

Use an interface as published linkage property

I'm trying to publish a property which allow me to choose between all components who implement a specified interface. Is possible to do something like this?

I tried using an interface as published property, but it doesn't seem to work. Here are steps I've followed:

I've defined two interfaces and three objects for testing:

uses
  Classes, Variants;

type
  IMyInterfaceA = interface
    function FGetValue() : Variant;
  end;

  TMyObjectA = class(TComponent, IMyInterfaceA)
  protected
    FValue : Variant;
    FAlternativeValueSource : IMyInterfaceA;
    function FGetValue() : Variant;
  published
    property Value : Variant read FGetValue write FValue;
    property AlternativeValueSource : IMyInterfaceA read FAlternativeValueSource write FAlternativeValueSource;
  end;

  IMyInterfaceB = interface
    procedure DoSomething();
  end;

  TMyObjectB = class(TComponent, IMyInterfaceB)
  public
    procedure DoSomething();
  end;

  TMyObjectC = class(TComponent);

implementation

function TMyObjectA.FGetValue() : Variant;
begin
  if((FValue = Null) AND (FAlternativeValueSource <> nil))
  then Result := FAlternativeValueSource.FGetValue
  else Result := FValue;
end;

procedure TMyObjectB.DoSomething();
begin
  //do something
end;

Then I've registered TMyObjectA, TMyObjectB and TMyObjectC in a design-time package:

procedure Register();
begin
  RegisterComponents('MyTestComponents', [
    TMyObjectA,
    TMyObjectB,
    TMyObjectC
  ]);

  //requires DesignIDE, uses DesignIntf
  RegisterPropertyInCategory('Linkage', TMyObjectA, 'AlternativeValueSource');
end;

I've added 4 objects to a Form:

MyObjectA1: TMyObjectA;
MyObjectA2: TMyObjectA;
MyObjectB1: TMyObjectB;
MyObjectC1: TMyObjectC;

Selecting MyObjectA1, in the AlternativeValueSource's dropdown list of the object inspector I see all objects which have an interface. (I was expecting just MyObjectA1 and MyObjectA2 which implement IMyInterfaceA)

enter image description here

Upvotes: 4

Views: 580

Answers (1)

Christophe Fardeau
Christophe Fardeau

Reputation: 224

Define a GUID to your Interface.

  IMyInterfaceA = interface
  ['{A5675798-F457-4E32-B0AA-608717CFD242}']
    function FGetValue() : Variant;
  end;

Delphi's IDE identify interface from their GUID (design time).

Upvotes: 4

Related Questions