I have two units and a class in each module that each other needs to work. How to avoid a circular link?

I have 2 classes, ClassAand ClassB, each in its own separate block, UnitAand UnitB.

UnitAuses UnitB. This allows you to ClassAcall the constructor for ClassB. However, part of the functionality ClassB'srequires the use of methods ClassA's. I can not use UnitBto use UnitA, otherwise it will cause a circular reference. What alternative do I provide ClassBfor accessing ClassA'smethods from UnitB?

+4
source share
3 answers

, . , , uses interface implementation.

:

UnitA;

interface

uses
  Classes, UnitB;

type
  TClassA = class(TObject)
  end;

...

.

UnitB;

interface

uses
  Classes;

type
  TClassB = class(TObject)
  end;

implementation

uses
  UnitA;

...

, , , ( ).

UnitA;

interface

uses
  Classes;

type
  TClassA = class;
  TClassB = class;


  TClassA = class(TObject)
  end;

  TClassB = class(TObject)
  end;

...

implementation. , .

UnitB;

interface

implementation

uses
  Classes, UnitA;

type
  TClassB = class(TObject)
  end;

...

, . , , , .. - .

+8

, (, , ) - , ( TObject), .

interface

TClassA = class
...
fRefToClassB : TObject;  // internal ref
...
procedure UsingBRef( pVar : TObject );
...
end;

implementation

procedure TClassA.UsingClassB
begin
  with fRefToClass as TClassB do
...
end;

procedure TClassB.UsingBRef( pVar : TObject );
begin
  with pVar as TClassB do
...
end;

, , . , , , .

+2

ClassA ClassB, ClassA, ClassA ( , ).

0

Source: https://habr.com/ru/post/1584992/


All Articles