2

I have two big class that defined in two separated unit. there is members in both class that linked to another. Forward declaration is not useful because classes is not in the same type block.

Is there any solution except merging this two?

Unit1:

uses
   Unit2;

type
   TclassA = class
     Method1: TClassB;
   end;

Unit2:

type
   TclassA = class;      //Type TclassA is not yet completely defined
   TclassB = class
     Method2: TClassA;
   end;

implementation
uses
   Unit1;
SAMPro
  • 1,068
  • 1
  • 15
  • 39
  • http://stackoverflow.com/questions/2644973/getting-around-circular-references-in-delphi – J... Jul 11 '15 at 10:52
  • 1
    http://stackoverflow.com/questions/2356318/delphi-enterprise-how-can-i-apply-the-visitor-pattern-without-circular-referenc – J... Jul 11 '15 at 10:53
  • So, no, there is no way to do this with the classes in separate units. Any alternative would require a change to the design, either declaring base classes in a different unit and deal with type casting, switching to using interfaces, or moving the two classes into the same unit, etc. – J... Jul 11 '15 at 10:55
  • Thanks j, I overcome this with type casting. – SAMPro Jul 12 '15 at 04:15
  • What about the question you asked? – David Heffernan Jul 12 '15 at 17:45
  • I thought this may have a direct solution, but as you said it's an unbreakable rule so in this situation where I don't want to merge them the type casting is a good solution. Simply define problematic properties as TObject in interface and type casting them in the implementation. – SAMPro Jul 13 '15 at 04:40
  • @SAMPro I dare to presume that David is suggesting that, although you have found a solution to your problem which is not the answer to your stated question, if the given answer below does provide the correct answer to your stated question then you should accept it as the correct answer. – J... Jul 13 '15 at 10:47
  • Thanks for clarification, excuse me I have problem with English but would any alternatives be a solution (like type casting you suggest) ? – SAMPro Jul 13 '15 at 23:37

1 Answers1

1

Is there any solution except merging this two?

No. This is an unbreakable rule. If two types refer to each other, and both need to be declared in the interface section, then they must be defined in the same unit.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490