?
Solved

Circular reference problem

Posted on 2002-05-10
5
Medium Priority
?
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

771 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