?
Solved

Circular reference problem

Posted on 2002-05-10
5
Medium Priority
?
411 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:micja491
5 Comments
 
LVL 8

Expert Comment

by:Cesario
ID: 7000722
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
 
LVL 9

Expert Comment

by:ITugay
ID: 7000935
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
 
LVL 10

Accepted Solution

by:
Jacco earned 800 total points
ID: 7001720
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
 
LVL 9

Expert Comment

by:ITugay
ID: 7004194
>>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
 

Author Comment

by:micja491
ID: 7029875
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question