Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can not use procedural with Object interfaces ?

Status
Not open for further replies.

mnavahan

Technical User
Apr 11, 2003
12
IR
i have some probelm with use procedural from one object interface :

unit Unit2;

interface

uses
Windows, Messages, SysUtils,Classes, Controls, Forms,Dialogs;

type
TDoProcedure = procedure(const FldName : string) of object;

IOneInterface = interface(IInterface) //universal way for any Direct DB
Worker
['{4BFEE200-1665-4F23-B430-D0076CD164EA}']
procedure DoIt(const FldName : string);
end;

TClassI = class(TInterfacedObject,IOneInterface)
public
procedure DoIt(const FldName : string);
constructor Create;
end;

TOtherClass = class(TObject)
public
constructor Create(DoJob : TDoProcedure);
end;

TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

constructor TClassI.Create;
begin
inherited;
end;

procedure TClassI.DoIt(const FldName : string);
begin
ShowMessage(FldName);
end;

constructor TOtherClass.Create(DoJob : TDoProcedure);
begin
DoJob('test');
end;

procedure TForm2.FormCreate(Sender: TObject);
var
IClass : IOneInterface;
AotherClass : TOtherClass ;
begin
IClass := TClassI.Create as IOneInterface;
AotherClass := TOtherClass.Create(IClass.DoIt);
//<--- ERROR [Error] Unit2.pas(64): Not enough actual parameters
end;

end.

what is problem in line: AotherClass := TOtherClass.Create(IClass.DoIt);
what is my problem ?
how Resolve it ?
if can not do it how callback when using interface ?

Thx

 
You're setting the interface object as the owner of AotherClass. I don't know if it can be done.

If i'm not going totally the wrong way here i think you should declare AotherClass :TClassI;

JP
 
Normally, the parameter in the Constructor identifies the owner of the object being created. I can think of two things to try...

1. Add "Override" to the constructor in the class declaration:
Code:
  TOtherClass  = class(TObject)
  public
    constructor Create(DoJob : TDoProcedure) override;
  end;
  ...
  constructor TOtherClass.Create(DoJob : TDoProcedure);
  begin
    inherited;
    DoJob('test');
  end;

If that doesn't work, try:
2. Make DoJob a property and set it after creating the object:
Code:
  TOtherClass  = class(TObject)
  private
    fDoJob: TDoProcedure;
  public
    constructor Create;
    property DoJob: TDoProcedure read fDoJob write fDoJob;
  end;
  ...
  constructor TOtherClass.Create(DoJob : TDoProcedure);
  begin
    inherited;
  end;
  ...
  procedure TForm2.FormCreate(Sender: TObject);
  var
    IClass      : IOneInterface;
    AotherClass : TOtherClass  ;
  begin
    IClass  := TClassI.Create as IOneInterface;
    AotherClass := TOtherClass.Create(self);
    AnotherClass.DoJob := IClass.DoIt;
    AnotherClass.DoJob('test');
   end;

-Dell





A computer only does what you actually told it to do - not what you thought you told it to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top