Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to assign an "of object" procedure to a "reference to" procedure?

I have :

  TMyProc = reference to procedure(const adata: String);

  TMyObject = Class(Tobject)
  private
    FMyProc: TMyProc ;
  public
    Property MyProc: TMyProc read FMyProc write FMyProc;
  end;

and

  TAnObject = Class(Tobject)
  public
    procedure MyProcImpl(const adata: integer); overload;
    procedure MyProcImpl(const adata: String); overload;
  end;

I want to do something like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   MyObject.MyProc := AnObject.MyProcImpl;

but I get Incompatible types: 'TMyProc' and 'Procedure of object'. How can I do ?

>Solution :

The problem is the ambiguity introduced by the overloads. Without the overloads your code would work fine.

You can disambiguate the overloads by using a temporary variable that is typed to match the overload you want to select:

program SO70183946;

type
  TMyProc = reference to procedure(const adata: string);
  TMyProcOfObject = procedure(const adata: string) of object;

  TMyObject = class
  private
    FMyProc: TMyProc;
  public
    property MyProc: TMyProc read FMyProc write FMyProc;
  end;

  TAnObject = class
  public
    procedure MyProcImpl(const adata: integer); overload;
    procedure MyProcImpl(const adata: string); overload;
  end;

procedure TAnObject.MyProcImpl(const adata: integer);
begin
end;

procedure TAnObject.MyProcImpl(const adata: string);
begin
end;

var
  MyObject: TMyObject;
  AnObject: TAnObject;
  MyProcOfObject: TMyProcOfObject;

begin
  MyProcOfObject := AnObject.MyProcImpl;
  MyObject.MyProc := MyProcOfObject;
end.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading