Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-07

Overriding properties

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Timothy Groves

Is there any way to override the type of a property? As an example, if I have written a list class:

type
     tList = class (tObject)
         private
             t_current : tObject;
             t_items : array of tObject;
         public
             property Current : tObject read t_current write t_current;
     end;

(this example is clearly not complete) and I want to create a descendant type that uses a different object, without worrying about typecasting. Is there a way to override Current? I tried generics, but using such renders me unable to access the list items' properties, making it impossible to sort the list.

Reply by email

Sven Barth
to Timothy Groves
"Timothy Groves" wrote
Is there any way to override the type of a property? As an example, if I

have written a list class:

Show quoted text 11 lines
type tList = class (tObject) private t_current : tObject; t_items : array of tObject; public property Current : tObject read t_current write t_current; end; (this example is clearly not complete) and I want to create a descendant

type that uses a different object, without worrying about typecasting. Is there a way to override Current? I tried generics, but using such renders me unable to access the list items' properties, making it impossible to sort the list.

Normally you simply declare the property anew in your child class. Look at TObjectList from unit contnrs for an example.
In your specific case you'll need a Getter-method that returns "TWhatEverSubclass(inherited Current)".

What exactly is your problem with using generics?

Regards
Sven

Reply by email

Timothy Groves
to Sven Barth
Sven Barth wrote 27 lines
Am 09.07.2014 18:43 schrieb "Timothy Groves" < >: > > Is there any way to override the type of a property? As an example, if I have written a list class: > > type > tList = class (tObject) > private > t_current : tObject; > t_items : array of tObject; > public > property Current : tObject read t_current write t_current; > end; > > (this example is clearly not complete) and I want to create a descendant type that uses a different object, without worrying about typecasting. Is there a way to override Current? I tried generics, but using such renders me unable to access the list items' properties, making it impossible to sort the list. Normally you simply declare the property anew in your child class. Look at TObjectList from unit contnrs for an example. In your specific case you'll need a Getter-method that returns "TWhatEverSubclass(inherited Current)".

Thanks, that helped me figure it out.

What exactly is your problem with using generics?

I can't access properties of whatever data type I used to specialize. So it's difficult to write a class that contains a list, that allows me to sort that list.

Reply by email

Sven Barth
to Timothy Groves
"Timothy Groves" wrote
> What exactly is your problem with using generics? I can't access properties of whatever data type I used to specialize. So

it's difficult to write a class that contains a list, that allows me to sort that list.

Do you have an example? Maybe you're misunderstanding something. You can also take a look at TFPGList in unit fgl or the stl units in packages/fcl-stl.

Regards,
Sven

Reply by email