Sign in Join

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

Mailing lists / fpc-pascal / 2008-01

Heterogenous list of objects

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Unknown author

Hi,

I'd like to manage a list of heterogenous objects. For instance, I have a list of objects to buy in store: cheeses (of class TCheese), soaps (of class TSoap), flowers (of class TFlower), etc...

I thought to gather pointers to different objects of these classes in a TList. But, when it comes to manipulate or destroy these items, I am in trouble: I have lost the information about the precise type of the object the pointer points to.

Does somebody have an idea?

Thanks in advance,
Gilles Marcou

Michael Van Canneyt
to Unknown author
Show quoted text 12 lines
Hi, I'd like to manage a list of heterogenous objects. For instance, I have a list of objects to buy in store: cheeses (of class TCheese), soaps (of class TSoap), flowers (of class TFlower), etc... I thought to gather pointers to different objects of these classes in a TList. But, when it comes to manipulate or destroy these items, I am in trouble: I have lost the information about the precise type of the object the pointer points to. Does somebody have an idea?

Use TObjectlist from contnrs.pas

Michael.

Damien Gerard
to Unknown author: I'd like to manage a list of heterogenous objects. For instance, I have a list of objects…
Show quoted text 13 lines
Hi, I'd like to manage a list of heterogenous objects. For instance, I have a list of objects to buy in store: cheeses (of class TCheese), soaps (of class TSoap), flowers (of class TFlower), etc... I thought to gather pointers to different objects of these classes in a TList. But, when it comes to manipulate or destroy these items, I am in trouble: I have lost the information about the precise type of the object the pointer points to. Does somebody have an idea?

Like this :
(I can make a complete example if needed)

type
   TNodeAbstract = class
      procedure print;virtual;abstract;
   end;
   TNodeFoo = class(TNodeAbstract)
      procedure print;override;
      procedure SpecificMethodeForFoo;
   end;

...

var objs: TObjectList;
objs := TObjectList.Create(True);
objs.Add(TNodeFoo.Create);

...

var o : TNodeAbstract;
for i := 0 to objs.Count - 1 do begin
    o := TNodeAbstract(Objs[i]);
    if (o is TNodeFoo) then
       (o as TNodeFoo).SpecificMethodeForFoo;
    o.Print;
end;
Show quoted text 7 lines
Thanks in advance, Gilles Marcou _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Damien Gerard Le temps n'a pas d'importance. Seul le code est important -- (f00ty)
Unknown author

Hi all,

thanks for your kind and fast answer. Effectively, TObjectList is what I need. Thanks to Damien, I understand a bit better how it works. But in my case I believe that I need to use some kind of TList of TObjectList. So is it possible to build a class that will have TObjecList member so that I will be able to use TList of this class?

For instance:

-----
TToDo=class(TList)
private
   fProducts: TObjectList;//Cheeses, Soaps, etc.
   fShops: TObjectList;//Shops, Markets, Internet,...
public
   property Products: TObjectList read fProducts write fProducts;
   property fShops: TObjectList read fShops write fShops;
   creator Create;
   destructor Destroy; override;
   procedure Clear; override;
   ...
end;
-----

I may have to say more about my project so you'll have better insight in my design problem. I have an XML to drive my application to do a set of diverse actions.

Root
|
|-action1-|-action11
| |-action12
|
|-action2-|-action21
|-action22

action1 and action2 shall be a TObjectList because action11, action12,... can be instances of different classes. Then I want action1 and action2 to be stored in some kind of container (I believe a TList).

So I call to your opinion to see if it is an acceptable design and if I am not mixing incompatible concepts (TList and TObjectList).

Thanks for your insight,
Gilles Marcou

Damien Gerard
to Unknown author
Show quoted text 10 lines
Hi all, thanks for your kind and fast answer. Effectively, TObjectList is what I need. Thanks to Damien, I understand a bit better how it works. But in my case I believe that I need to use some kind of TList of TObjectList. So is it possible to build a class that will have TObjecList member so that I will be able to use TList of this class? For instance:

No. Per node you can add its own ObjectList and use virtual methods to make the job for you.

Damien Gerard Le temps n'a pas d'importance. Seul le code est important -- (f00ty)