2

I am using delphi 2009 and VCL components. I have created a collection called TStreets made of items TStreet which has just two private fields. Now I need to add to Tstreet class another field/property to keep track (by using reference) of other objects of class TMyObject.

An example: let's assume that TStreet collection contains five elements and ten objects (TMyObject) exists in my application at run-time. Each objects of TMyObject can belong to only one TStreet so I need to save for each TStreet all reference of objects and then be able to move one or more object reference from one TStreet to another. Should I create another colletion under TStreet where saving object references?

Is it correct the way to go?

marcostT
  • 573
  • 1
  • 10
  • 20
  • 1
    Are you actually using `TCollection` and `TCollectionItem`? Those classes are designed to help publish collections to the Object Inspector. Unless you actually want your Streets and YourObjects to be dropped on a form and manipulated at design time, you don't want `TCollection` – Cosmin Prund Feb 14 '11 at 16:00
  • 1
    I can't make any sense of this. Perhaps I'm being particularly dumb today, but I wonder if you could try to make your question a little clearer. – David Heffernan Feb 14 '11 at 16:18
  • 1
    There is nothing wrong with using TCollection as collection-type container, regardless of form designer interoperability. – Free Consulting Feb 14 '11 at 17:34
  • "There is nothing wrong with using TCollection as collection-type container". Well, other than being compelled to make your members descend from `TCollectionItem` and having to take `TCollection`'s. rather designer-centric interface. `TObjectList` would be my personal choice. – David Heffernan Feb 14 '11 at 17:43

1 Answers1

4

Given the following.

TMyObject = class
  ...
end;

TStreet = class
 ...
 public
   property MyObject : TMyObject ...;
end;

TStreets = TList<TStreet>;

It appears from reading your question that a TMyObject can only be tied to one TStreet.

Then I would recommend reversing the references.

TStreet = class;

TMyObject = class
protected
  FStreet : TStreet;
public
  property Street : TStreet read FStreet write FStreet;
end;

TMyObjectList = TList<TMyObject>;

TStreet = class
 private
   // Looks through MyObjectList returning correct 
   function GetMyObjecty : TMyObject; reference.
 public
   property MyObject : TMyObject read GetMyObject;
   // Reference to list that contains all instance of TMyObjectList.
   property MyObjectList : TMyObjectList; 
end;

TStreets = TList<TStreet>;

TMyObjectList = TList<TMyObject>;
Robert Love
  • 12,447
  • 2
  • 48
  • 80