I know this is old and I am using Delphi 5. Not sure if it is the same in later version of Delphi.
I found that if an object delegate its interface implementation to another object, the original object canonot call the interfaced method directly, which I expect what delegation interface implementation should be.
Here is my question
IFoo = interface
procedure InterfaceMethod;
end;
TBar = class(TComponent, IFoo)
FFoo: IFoo;
procedure ObjectMethod;
property Foo: IFoo read FFoo implements IFoo;
end;
TBar.Method2
begin
InterfaceMethod; // this will give compile error, Method1 not declared!
(Self as IFoo).InterfaceMethod; // compile error, saying operation not supported.
FFoo.InterfaceMethod; // this work, but meaningless, since this can be any object, why the need of interface!
end;
Similar if I do this outside of the TBar class. Say
Bar := TBar.Create;
Bar.InterfaceMethod; // compile error
(Bar as IFoo).InterfaceMethod; // compile error
Bar.Foo.Method1; // this work, but...
Can anyone explain why is it this way or my understand is not correct. Or may be this will be correct after D5?
The following are sample code
unit iwSqlDbEngine;
//
// Database resource and helper funcions.
//
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ScktComp, ExtCtrls;
type
ISqlMaker = interface
['{5A671E7B-957B-4A52-BB5E-87EEA8E5687B}']
procedure SetSqlStatement(const value: string);
function GetSqlStatement: string;
// Insert and update
function Insert(table: string): ISqlMaker;
function Update(table: string): ISqlMaker;
function AddField(fieldname: string; value: variant; isExpr: Boolean = False): ISqlMaker; overload;
function AddField(field: TField): ISqlMaker; overload;
// Delete
function Delete(table: string): ISqlMaker;
// Select
function Select(table: string; columns: array of string): ISqlMaker;
function Join(table: string; columns, matches: array of string): ISqlMaker;
function LeftJoin(table: string; columns, matches: array of string): ISqlMaker;
// Other clauses
function Where(aExpr: string): ISqlMaker;
function GroupBy(columns: array of string): ISqlMaker;
function OrderBy(columns: array of string): ISqlMaker;
function Having(vExprs: array of string): ISqlMaker;
property SqlStatement: string read GetSqlStatement write SetSqlStatement;
end;
TSampleDbEngine = class(TComponent, ISqlMaker)
private
FSqlMaker: ISqlMaker;
public
procedure Execute;
property SqlMaker: ISqlMaker read FSqlMaker write FSqlMaker implements ISqlMaker;
end;
implementation
procedure TSampleDbEngine.Execute;
begin
(Self as ISqlMaker).GetSqlStatement;
end;
end.
Same compile error as described above.