delphicustom-componentcircular-dependencydelphi-units

Circular reference issue with Classes which use each other


I have the following two classes:

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;


TcmTPDataConnector = class(TComponent)
  private
    FDatabase: TnxDatabase;
    FObservers: TList;
    FTableForCategories: TnxTable;
    FTableForItemCategoryLinks: TnxTable;
    FTableForItems: TnxTable;
    procedure SetTableForItemCategoryLinks(const Value: TnxTable);
  protected
    procedure IterateObservers;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Register(Instance: TcmTPDataPanel);
    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;

TcmDataPanel is a (3rd generation) TFrame descendent. It's not used directly, but rather multiple other compound components descend from it.

TcmDataConnector is a component which holds the database connection information for these other components.

I'm using ModelMaker, and am trying to implement an Observer pattern so that multiple "Data Panels" can be automatically updated whenever the "Data Connector" changes state.

Here's my problem: I can't seem to get around the "circular refernece" problem, as each class uses the other in the Interface section. Putting the associated "uses" reference in the Interface section doesn't clear up the "Undeclared identifier" issue, and putting them both in the same unit doesn't either. Right now they are in the same unit (I've tried them in separate units, and together), and I get the "Undeclared identifier: TcmTPDataConnector" error.

How can I get around this?

================
UPDATE:

It's Friday night at 9:30pm where I am, and I was totally stuck. Just MINUTES after my post, you people SAVED THE DAY!! (night!) THANK YOU!!

Man, I love StackOverflow.... SO my friend in this learning-like-crazy season. : ) Thank you, thank you, thank you...


Solution

  • On the same unit, try the following:

    TcmTPDataConnector = class(TComponent);
    
    
    TcmTPDataPanel = class(TcmTPBasePanel)
       ... 
    end;
    
    TcmTPDataConnector = class(TComponent)
      ...
    end;