On Thu, Sep 15, 2011 at 12:12 PM, Felipe Monteiro de Carvalho <felipemonteiro.carva...@gmail.com> wrote: > I don't understand why it would be wrong. For me it seams correct and > similar to this example: > > http://wiki.lazarus.freepascal.org/How_To_Use_Interfaces_to_write_less_code
I'd like to use the same idea of link above, ie, using a real class (and your's own methods and properties) but assign the instance of this object, in an interface. But, like the link, if I can not use Free method on my objects... well, I think this is strange. What I want was: Two classes completly different but that implement the same interface... and these classes will be instanciated using variables with the same type. To solve my problem, the only way is create more interfaces -- and more code, unfortunately. See below. 'a' and 'b' are different objects, different methods, but they share the same methods, ie, GetInfo and SetInfo. program p1; {$apptype console} {$ifdef FPC} {$mode objfpc}{$H+} {$endif} uses heaptrc, Classes, SysUtils; type IInfo = interface ['{F71A4131-E0A5-48CB-B563-7BBB079D1085}'] function GetInfo: string; procedure SetInfo(const AValue: string); end; IInfoA = interface(IInfo) ['{0E816EA4-21A5-49B3-B7E8-396E2F788E4B}'] procedure SetName(const AValue: string); end; IInfoB = interface(IInfo) ['{0E816EA4-21A5-49B3-B7E8-396E2F788E4B}'] procedure SetNumber(const AValue: Integer); end; TInfo = class(TInterfacedObject, IInfo) private FInfo: string; public function GetInfo: string; procedure SetInfo(const AValue: string); end; TInfoA = class(TInfo, IInfoA) public procedure SetName(const AValue: string); end; TInfoB = class(TInfo, IInfoB) public procedure SetNumber(const AValue: Integer); end; TObj = class(TObject) Info: IInfo; end; { TInfo } function TInfo.GetInfo: string; begin Result := FInfo; end; procedure TInfo.SetInfo(const AValue: string); begin FInfo := AValue; end; { TInfoA } procedure TInfoA.SetName(const AValue: string); begin SetInfo('InfoA :: ' + AValue); end; procedure TInfoB.SetNumber(const AValue: Integer); begin SetInfo('InfoB :: ' + IntToStr(AValue)); end; procedure Run; var a: IInfoA; b: IInfoB; o: TObj; begin a := TInfoA.Create; a.SetName('ZEOS'); b := TInfoB.Create; b.SetNumber(99); o := TObj.Create; o.Info := a; writeln(o.Info.GetInfo); o.Info := b; writeln(o.Info.GetInfo); o.Free; end; begin Run; end. _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal