Link to home
Start Free TrialLog in
Avatar of micja491
micja491

asked on

Circular reference problem

Hello, I have a problem with developing an elegant object oriented application in Delphi 6.

For example I need two classes TSalesperson and TCustomer where each instance of a salesperson needs to know what customers it is responsible for and each customer-instance need to know what salesperson they belong to.

The natural thing would be to do the following:

unit uSalesperson;

implementation

uses
  uCustomer;

type
  TSalesperson = class
  private
    FCustomer: TCustomer;
  end;

==========================================

unit uCustomer;
implementation

uses
  uSalesperson;

type
  TCustomer = class
  private
    FSalesperson: TSalesperson;
  end;

When I do this and have each class residing in it's own unit I obviously get a circular reference warning from the compiler.

I can think of two workarounds for this problem:
1. Put both classes in the same unit
2. Make the fields of typ TObject and cast the references to the correct classes at runtime, in that case i could make one unit use(uses) the other in the implementation-section instead of in the interface and the compiler would be happy.

But I don't like either of these solutions because they will give me problems later on.

so my question is:
Is there any other way to solve this problem, perhaps by using interfaces or anything else? I'd be happy for any ideas.

Thanks
Mike
Avatar of Cesario Lababidi
Cesario Lababidi
Flag of Germany image

Very Simple ;-)

Add a new Unit ( UNIT0.PAS );

Unit UNIT0.PAS;

Interface
  uses Objects,Classes;

Type
   TSalesperson = Class;
   TCustomer    = Class;


Implentation
End.

Now add the Unit0 to Unit1 and Unit2 and it works

good luck

Cesario
   
Hi micja491,

I suppose that there is no problems with warning, this is not error, right? It is standard way to let one unit know about another placing USES clause bellow  IMPLEMENTATION part.  So, why you care about warnings?

------
Igor.
ASKER CERTIFIED SOLUTION
Avatar of Jacco
Jacco
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>ITugay: In D5 it is an error no warning.

do you mean that it will produce error in D5?

//------------------------------
unit Unit2;

interface

implementation

uses
  Unit3;

end.


//------------------------------
unit Unit3;

interface

implementation

uses
  Unit2;

end.


btw, looking and Delphi's VCL source, you can notice that all classes which has cross-references, located in the same unit.

I would do by the same way, e.g:

type
  TCustomer = class;
  TSalesPerson = class;

  TCustomer = class(TObject)
    FSalesPerson: TSalesPerson;
  end;

  TSalesPerson = class(TObject)
    FCustomer: TCustomer;
  end;

-----
Igor.
 


Avatar of micja491
micja491

ASKER

Thanks all of you for your comments:

Cecario:
The forward declaration solution you recommended
Type
  TSalesperson = Class;
  TCustomer    = Class;

doesn't work as Jacco pointed out (unless of course you put the Forward declarations in the same unit and section as the full class declarations, but then we're back to square one.

ITugay:
You are right that if you put the uses-clauses in the implementation part of the unit then you don't get the circular reference error. But for two classes that need to reference each other I have to have the class declarations in the interface-section, Otherwise i won't be able to set the correct type of the reference-variables right?

And about putting all reference-classes in the same unit, it will make my code messy, since most of my major classes will need to reference each other so most of the apps code will be in one unit making it hard to navigate.

Jacco:
So far I think that your suggestion with the Interfaces is the neatest one. I don't really mind the extra work of creating interfaces but then I'd have to put all interfaces in the same Unit, but that's quite a bit less messy since the Interfaces contain no impementation code.

So if no-one has any other suggestions I'll try this out and accept this comment in a couple of days.

/Mike