【问题标题】:delphi TCollection count not increasing after Add添加后delphi TCollection计数不增加
【发布时间】:2011-03-27 03:20:00
【问题描述】:

我有类型

  • TNotifyReply = 类(TCollectionItem)
  • TNotifyReplylist = 类(TOwnedCollection)

NotifyReplylist := TNotifyReplylist.Create(self, TNotifyReply);

调用此函数后(任意次),计数仍为零

function TNotifyReplylist.addItem: TNotifyReply;
 begin
   Result := inherited Add as TNotifyReply;
   OutputDebugString(PAnsiChar('Count > '+ inttostr(count)));
 end;

知道这里发生了什么吗?

【问题讨论】:

  • 您的代码按预期工作。当我使用 D2009 时,只需将 PAnsiChar 更改为 PChar。不过,不要认为 TCollection(Item) 自 D7 以来发生了很大变化。

标签: delphi delphi-7 tcollection townedcollection


【解决方案1】:

发现问题了,TNotifyReply.Create 是

constructor TNotifyReply.Create(ACollection: TCollection);
begin
  inherited Create(Collection);
  ....

改为

inherited Create(ACollection);

【讨论】:

  • 这实际上是人们常犯的一个错误。 :-) 我的建议?将类中的 Collection 变量重命名为 FCollection。 (除非它是一个属性,但话说回来,您是否希望程序员可以直接访问该集合?)
  • @Workshop,Collection 是基类 TCollectionItem 的属性。您确实希望开发人员能够直接访问它,否则项目将不知道属于谁。使用构造函数参数中的值分配属性。
猜你喜欢
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
相关资源
最近更新 更多