当前位置:  开发笔记 > 编程语言 > 正文

组件创建问题:字段最终为零

如何解决《组件创建问题:字段最终为零》经验,为你挑选了1个好方法。

这是我在这里工作的项目的延续: 与相互使用的类的循环引用问题

在那里收到的建议修复了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"行.他们应该被取消评论吗?

PS显然,我还在学习组件创建和继承.欢迎任何其他意见和建议.

PPS今天有很多问题要问我.如果我需要将它降低一级,请随时告诉我....(这里只有很多问题的奖励).

在此先感谢您的帮助!:)



1> Nick Hodges..:

您需要覆盖您的构造函数,然后在该构造函数中调用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;

推荐阅读
mylvfamily
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有