[fpc-pascal] Is there a way to avoid class casting?

Graeme Geldenhuys graemeg at opensoft.homeip.net
Tue Jul 28 17:06:30 CEST 2009


Horacio Jamilis wrote:
> so, I can use... mylinkedproperty.obj.[prop] ... beeing [prop] any 
> property of the object that is of class LinkedObjectClass, and avoiding 
> to type TMyLinkeddObjectClass(mylinkedproperty.obj).[prop] ???


You can define TMyLinkeddObjectClass and override the property types so 
casting is only done in that class's implementation. All your instances 
of TMyLinkeddObjectClass doesn't have to do casting then.


eg:

   // you base class of your OPF framework.
   TtiObjectList = class(TtiObject)
   private
   protected
     function    GetItems(i: integer): TtiObject; virtual;
     procedure   SetItems(i: integer; const Value: TtiObject); virtual;
   public
     property    Items[i:integer]: TtiObject read GetItems write SetItems;
     function    Add(const AObject: TtiObject): integer; virtual;
   published
   end;


   // Your custom object list class which defines correct class types
   TMyLinkeddObjectClassList = class(TtiObjectList)
   private
   protected
     function    GetItems(i: integer): TMyLinkeddObjectClass; reintroduce;
     procedure   SetItems(i: integer; const Value: 
TMyLinkeddObjectClass); reintroduce;
   public
     property    Items[i:integer]: TMyLinkeddObjectClass read GetItems 
write SetItems;
     function    Add(const AObject: TMyLinkeddObjectClass): integer; 
reintroduce;
   published
   end;



Regards,
   - Graeme -

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




More information about the fpc-pascal mailing list