使用Tcollection在Dephi中保存vcl对象引用 [英] Saving vcl objects references in dephi with Tcollection
问题描述
我正在使用delphi 2009和VCL组件。我创建了一个由项目TStreet组成的名为
TStreets的集合,该集合只有两个私有字段。现在,我需要将
添加到Tstreet类的另一个字段/属性中,以跟踪(通过使用引用)类TMyObject的
个其他对象。
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.
一个例子:假设TStreet集合包含五个元素和十个对象,
(TMyObject)在运行时存在于我的应用程序中。 TMyObject的每个对象只能属于一个TStreet属于
,因此我需要为每个TStreet保存所有对象引用,然后
可以将一个或多个对象引用从一个TStreet移动到另一个TStreet。
是否应该在TStreet下创建另一个可保存对象引用的集合?
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?
这是否正确?
推荐答案
给出以下信息。
TMyObject = class
...
end;
TStreet = class
...
public
property MyObject : TMyObject ...;
end;
TStreets = TList<TStreet>;
从您的问题看来,TMyObject只能绑定到一个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>;
这篇关于使用Tcollection在Dephi中保存vcl对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!