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
micja491Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
JaccoConnect With a Mentor Commented:
Hi All,

Cesario: I get an error class TCustomer not yet completely defined

ITugay: In D5 it is an error no warning.

Here is a way to solve your problem (with interfaces):

unit Unit0;

interface

type
  ISalesPerson = interface
    procedure Test;
  end;

  ICustomer = interface
    procedure Test;
  end;

implementation

end.

unit Unit2;

interface

uses
  Unit0;
 
type
  TSalesPerson = class(TInterfacedObject, ISalesPerson)
  private
    fCustomer: ICustomer;
  public
    procedure Test;
  end;

implementation

{ TSalesPerson }

procedure TSalesPerson.Test;
begin
  fCustomer.Test;
end;

end.

unit Unit3;

interface

uses
  Unit0;
 
type
  TCustomer = class(TInterfacedObject, ICustomer)
  private
    fSalesPerson: ISalesPerson;
  public
    procedure Test;
  end;

implementation

{ TCustomer }

procedure TCustomer.Test;
begin
  fSalesPerson.Test;
end;

end.

Downside of this appraoch is that you have to maintain the interfaces and the classes which can be a slowdown when developing the classes.

Regards Jacco
0
 
CesarioCommented:
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
   
0
 
ITugayCommented:
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.
0
 
ITugayCommented:
>>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.
 


0
 
micja491Author Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.