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

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Fri Mar 13 11:16:39 CET 2015


On 2015-03-13 09:39, aradeonas wrote:
> About question you mean make a File class with name,size,artist,width
> and all others in one place?If this I didn't like it,It seem messy,

I agree, I wouldn't do it that way either. I would have subclasses which
gives more specific details of each media type.

 TBaseFile = class(TObject)
 published
   property Name: string...
   property Size: integer...
   property Caption: string...
 end;

 TVideoFile = class(TBaseFile)
 published
   property Width: integer...
 end;

 TAudioFile = class(TBaseFile)
 published
   property Artist: string...
 end;

In your listview somewhere
  ListView.Items.AddObject(obj.Caption, obj);

obj can be a TBaseFile, TVideoFile or TAudioFile instance.

Then in your listview's OnPaint event (on whatever event draws each row)
var
  obj: TBaseFile;
begin
  obj := ListView.Items[Listview.Selected] as TBaseFile;
  if obj is TAudioFile then
  begin
    // do something with TAudioFile(obj).Artist etc.
  end
  else if obj is TVideoFile then
  begin
    // do something with TVideoFile(obj).Width etc
  end
  else if obj is TBaseFile then
  begin
    // do whatever
  end;
end;


This is just one possible way of doing it. Interface can obviously also
be used in a similar way, where one interface descends from another.




Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/




More information about the Lazarus mailing list