This question has already been answered:

We have two classes in different modules:

Unit uClassA; Interface Type TClassA = Class(TObject) Private FBlocks: Array Of TBlock; Function FGetBlock(Value: Byte): TBlock; Public ClassB: TClassB; Property Block[Index: Byte]: TBlock Read FGetBlock; End; Implementation End. 

and ClassB, which is created in the ClassA constructor, after filling the FBlocks array

 Unit uClassB; Interface Type TClassB = Class(TObject) Public Procedure SetBlock(); End; Implementation Procedure TClassB.SetBlock(); Begin //Тут нужно достучаться до ClassA.Block[Index] End; End. 

How to organize it more correctly? It only comes to mind to make an empty class with this array and inherit both from it, but is this correct?

Reported as a duplicate by Kromster members, Community Spirit May 27 '18 at 11:20 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • To transfer as parameter in SetBlock ? - VTT
  • one
    Add a TClassB field such as Owner: TClassA and pass a pointer to the parent object in the constructor. - dr. FIN
  • @dr. FIN is a good idea, but the cross declaration is working out; I need to register uClassB in Uses uClassA and vice versa, which is impossible. - Isaev
  • one
    @Isaev add the third module to which your modules with TClassA and TClass B will refer. In this module, describe the IClassA interface with the necessary methods and implement this interface in TClassA . In the constructor, pass the link to the interface. - dr. FIN
  • 3
    The question is unclear. Add code, give an example of what is specifically missing. (and also why is there an emphasis on “after filling the array” and what is “reach out”?). Also check out stackoverflow.com/questions/625439 - Kromster

1 answer 1

 Unit blockArrayHolder; Interface Type TBlockArrayHolder = Class(TObject) Public Data: Array Of TBlock; End; Implementation End. Unit uClassA; Interface Type TClassA = Class(TObject) Private FBlocks: TBlockArrayHolder; Function FGetBlock(Value: Byte): TBlock; Public constructor Create; ClassB: TClassB; Property Block[Index: Byte]: TBlock Read FGetBlock; End; Implementation constructor TClassA.Create; begin FBlocks := TBlockArrayHolder.Create; // fill FBlocks.Data ClassB := TClassB.Create(FBlocks); end; End. Unit uClassB; Interface Type TClassB = Class(TObject) Private FBlocks: TBlockArrayHolder Public Procedure SetBlock(); constructor Create(aBlocks: TBlockArrayHolder); End; Implementation Procedure TClassB.SetBlock(); Begin //Тут нужно достучаться до ClassA.Block[Index] - через fBlocks.Data End; constructor TClassB.Create(aBlocks: TBlockArrayHolder); begin FBlocks := aBlocks; end; End. 
  • Again the code sheet without a single explanation ... - Kromster
  • one
    So everything is transparent, what is there to explain something?) - Isaev