这是我在这里工作的项目的延续: Circular reference issue with Classes which use each other
那里收到的建议修正了ciruclar引用问题(再次感谢您的帮助)。现在我正在与其他东西搏斗:TcmDataPanel.FObservingDataPanels总是最终= nil,显然是因为它永远不会被创建。 (最初我遇到了访问冲突,但在进一步测试时发现FObserver始终为零)。
以下是相关代码(它是TFrame单元,TcmTPDataPanel是TFrame descednent):
unit cmTPDataPanelFrame;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cmTPBasePanelFrame, cmTPPanels, nxdb, nxllComponent;
type
TcmTPDataConnector = class;
TcmTPDataPanel = class(TcmTPBasePanel)
Database: TnxDatabase;
Session: TnxSession;
private
FDataConnector: TcmTPDataConnector;
MyNxDataBase: TnxDatabase;
MyNxSession: TnxSession;
MyRefNxDataBase: TnxDatabase;
protected
procedure Disconnect; virtual; abstract;
procedure Refresh; virtual;
procedure Requery; virtual; abstract;
public
procedure Connect;
published
property DataConnector: TcmTPDataConnector read FDataConnector write
FDataConnector;
end;
TcmTPCustomDataConnector = class(TComponent)
private
FDatabase: TnxDatabase;
FObservingDataPanels: TList;
FTableForCategories: TnxTable;
FTableForItemCategoryLinks: TnxTable;
FTableForItems: TnxTable;
procedure SetTableForItemCategoryLinks(const Value: TnxTable);
protected
procedure IterateObservers;
public
constructor Create(AOwner: TComponent);
destructor Destroy; override;
procedure Register(Instance: TcmTPDataPanel);
procedure ReportObservers;
procedure Unregister(Instance: TcmTPDataPanel);
published
property Database: TnxDatabase read FDatabase write FDatabase;
property TableForCategories: TnxTable read FTableForCategories write
FTableForCategories;
property TableForItemCategoryLinks: TnxTable read
FTableForItemCategoryLinks write SetTableForItemCategoryLinks;
property TableForItems: TnxTable read FTableForItems write FTableForItems;
end;
TcmTPDataConnector = class(TcmTPCustomDataConnector)
end;
var
cmTPDataPanel: TcmTPDataPanel;
implementation
===和===
{
*************************** TcmTPCustomDataConnector ***************************
}
constructor TcmTPCustomDataConnector.Create(AOwner: TComponent);
begin
ShowMessage('TcmTPCustomDataConnector.Create entered.');
// inherited Create(AOwner); // TODO : check duplicate
FObservingDataPanels := TList.Create();
end;
destructor TcmTPCustomDataConnector.Destroy;
begin
FreeAndNil(FObservingDataPanels);
//inherited Destroy; // TODO : check duplicate
end;
我期望在cmTPDataConnector.Create上运行的ShowMessage行永远不会显示,这使我认为它不是从TcmTPCUstomDataConnector继承Create方法。不应该吗?
它“感觉”就像有一些明显的东西我不见了,但我没有看到它。 : - \
两个问题:
1)为什么没有创建FObservingDataPanels?
2)ModelMaker在某些时候输入了“//继承的Create(AOwner); // TODO:check duplicate”和“// inherited Destroy; // TODO:check duplicate”行。他们应该被取消评论吗?
P.S。显然,我还在学习组件创建和继承。欢迎任何其他意见和建议。
P.P.S。今天我的很多问题。如果我需要将它降低一级,请随时告诉我....(这里只有很多问题的奖励日)。
提前感谢您的帮助! :)
答案 0 :(得分:10)
您需要覆盖构造函数,然后在该构造函数中调用inherited作为/ first / thing。
public
constructor Create(AOwner: TComponent); override;
constructor TcmTPCustomDataConnector.Create(AOwner: TComponent);
begin
inherited Create(AOwner); // TODO : check duplicate
ShowMessage('TcmTPCustomDataConnector.Create entered.');
FObservingDataPanels := TList.Create();
end;
答案 1 :(得分:3)
我在Delphi上生锈了,但我认为你的构造函数声明可能需要“覆盖”。