Board index » delphi » 2 objects in 2 units refering to each other

2 objects in 2 units refering to each other

Here are three ways, depending on the circumstance, if you do not want
to put UA and UB in the same unit:

(1) Create a third unit with AbstractUA and AbstractUB in this third
unit. Put all the cross references at this level of abstraction.
Derive UA = class(AbstractUA) and UB = class(AbstractUB).

(2) Create "Events" to tie the two together instead. Essentially, pass
call back routines from one to the other.

(3) Create one or more shared hierarchies to link them. For example,
SharedUA is created in the create function of UA and destroyed in its
destructor and forwards functions to UA or performs them itself. Then
derive SharedUA from AbstractSharedUA as in (1).

Leo

On 26 Jun 1996 19:41:28 GMT, a...@sn.no wrote:

Quote
>I have 2 objects, each in their own unit. Both objects refer to each other.
>To accomplish this, it seems that I have to have the other unit in the uses
>statement of the interface part. This gives a circular reference, as stated in
>the manual.

>Is there some way to accomplish what I want, without having to make one
>unit out of the 2. More exactly, If I have to put these 2 units together, I have
>to do the same with a couple of other units too. This means that 4 relatively
>large units will have to become 1.

>In order to clarify this, I have made this example;

>  Unit UA;
>  Interface
>  Uses TB; {Gives circular reference}

>    TA = class(TObject)
>      ....
>    public
>      Property BObjects[Index : integer] : TB read getTB;
>      ....
>    end;
>  Implementation
>  .....
>  end.

>  Unit UB;
>  Interface
>  uses UA; {gives circular reference}
>    {TA = class; -> try to make a forward class declaration
>                -> stops at implementation part, not declared}
>    TB = class(TObject)
>      ....
>    public
>      constructor create(Owner : TA);
>      ....
>    end;
>  Implementation
>    {Uses UA -> Could hopefully be used, but has to come before declaration,
>               else the object refered is unknown for the compiler}
>  .....
>  end.

>Putting TA = Class; in interface part of Unit UB to make a forward class
>declaration (then I could put one of the uses part in the implementation section)
>does not help, since the class has to be declared in the interface part.

>So. Is this impossible without having those mutually dependent objects in one
>unit?

>Thank you in advance, for any viewpoints of this problem.

>Alf Inge Lindtner
>a...@sn.no

>----------------------------------------------
>http://aksess-www.sn.no/~alfi/

 

Re:2 objects in 2 units refering to each other


Helo Alf,

Put at least one "uses" clause in implementation part of unit
(rather then interface part). For more info read the manual under
"circular reference".

Pawel

--
\||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||/
-         Pawel Ksiezyk     ksie...@bull.mimuw.edu.pl        -
-    Institute of Informatics   Warsaw University   Poland   -
/||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\

Other Threads