Solved

Circular reference problem

Posted on 2002-05-10
5
390 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 200 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How can I open IE in olecontainer? 7 109
Wininet read php file from internet issue 6 70
delphi exception 7 58
LAN or WAN ? 11 63
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now