[Lazarus] How to cast a Interface to a Object in Lazaeus

aradeonas aradeonas at operamail.com
Thu Mar 12 13:28:46 CET 2015


Here an example for you:

type

> { IFile }
>
> IFile = interface function GetName: string; procedure SetName(AValue:
> string); property Name: string read GetName write SetName; end;
>
> TFileList=specialize TFPGList<IFile>;
>
>
> { TVideoFile }
>
> TVideoFile = class(TInterfacedPersistent, IFile) private FName:
> string; FWidth: integer; function GetName: string; procedure
> SetName(AValue: string); public property Name: string read GetName
> write SetName; property Width: integer read FWidth write FWidth; end;
> { TAudioFile }
>
> TAudioFile = class(TInterfacedPersistent, IFile) private FName:
> string; FArtist: string; function GetName: string; procedure
> SetName(AValue: string); public property Name: string read
> GetName write SetName; property Artist: string read FArtist write
> FArtist; end;
>
> var Form1: TForm1;
>
> implementation
>
> {$R *.lfm}
>
> { TAudioFile }
>
> function TAudioFile.GetName: string; begin Result:=FName; end;
>
> procedure TAudioFile.SetName(AValue: string); begin
> FName:=AValue; end;
>
> { TVideoFile }
>
> function TVideoFile.GetName: string; begin Result:=FName; end;
>
> procedure TVideoFile.SetName(AValue: string); begin
> FName:=AValue; end;
>
>
> { TForm1 }
>
> procedure TForm1.FormCreate(Sender: TObject); var V1,V2:TVideoFile;
> A1,A2:TAudioFile; Fl:TFileList; begin Fl:=TFileList.Create;
> V1:=TVideoFile.Create; V1.Name:='V'; V1.Width:=100;
> A1:=TAudioFile.Create;
> A1.Name:='A';
> A1.Artist:='Art'; FL.Add(V1); FL.Add(A1); V2:=TVideoFile(FL.Items[0]);
> A2:=TAudioFile(Fl.Items[1]); ShowMessage(V2.Name);
>    ShowMessage(A2.Name); end;

What you think?

Ara

On Thu, Mar 12, 2015, at 05:26 AM, Marcos Douglas wrote:
> On Thu, Mar 12, 2015 at 8:39 AM, aradeonas
> <aradeonas at operamail.com> wrote:
> >
> > For example I have interface for filetypes that have type,size,name
> > but have class for video,audio and picture and I want to add them in
> > the list and then get them and get name and size and other
> > propertice considering type. Regards, Ara
>
> So work using objects directly. Why do you want to use interfaces if
> you will use casting for a concrete class?
>
> Marcos Douglas
>
> --
> _______________________________________________
> Lazarus mailing list Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


-- 
http://www.fastmail.com - A no graphics, no pop-ups email service

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20150312/315fdaa1/attachment-0003.html>


More information about the Lazarus mailing list