[Lazarus] OOP basics - 2

John vd Waeter john at jvdw.nl
Wed Apr 14 17:28:25 CEST 2010


You cannot refer to unitX in the interface section of unitY AND at the 
same time refer to unitY in the interface section of unitX.

The compiler sees unitX needs unitY and so will try to compile unitY 
first. In unitY it sees unitX is needed and wants to compile unitX first 
and so on. Hence: circular reference.

If unitX needs unitY and vice versa, at least in one of them the 
reference to the other unit should be moved to the implementation section.

e.g:

unit unitX;
interface
uses unitY
implementation
var...
begin
end;

unit unitY
interface
...
implementation
uses unitX
var...
begin
end;

John


Kjow wrote:
> 2010/4/14 Michael Van Canneyt <michael at freepascal.org>:
>> Put unit2 in the implementation uses list of unit1. Then it should work.
> 
> Other question:
> 
> if I have a similar situation, it is possible to implement?
> 
> ____________________
> 
> unit Unit1;
> 
> {$mode objfpc}{$H+}
> 
> interface
> 
> uses
>   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics,
> Dialogs, unit3;
> 
> type
> 
>   { TForm1 }
> 
>   TForm1 = class(TForm)
>     procedure FormCreate(Sender: TObject);
>   private
>     { private declarations }
>   public
>     { public declarations }
>     x,y: integer;
>     tester1: TTest;
>   end;
> 
> var
>   Form1: TForm1;
> 
> implementation
> 
> uses
>   unit2;
> 
> {$R *.lfm}
> 
> { TForm1 }
> 
> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   init(5, 2, Form1);
>   ShowMessage('Result: '+IntToStr(tester1.test(x,y,Form1)));
> end;
> 
> end.
> ____________________
> 
> unit Unit2;
> 
> {$mode objfpc}{$H+}
> 
> interface
> 
> uses
>   Classes, SysUtils, unit1, unit3;
> 
> procedure init(a,b: integer; MainForm: TForm1);
> 
> implementation
> 
> var
>   tester2: TTest;
> 
> procedure init(a,b: integer; MainForm: TForm1);
> begin
>   MainForm.x:=tester2.test(a,b,MainForm);
>   MainForm.y:=-tester2.test(-a,-b,MainForm);
> end;
> 
> end.
> ____________________
> 
> unit Unit3;
> 
> {$mode objfpc}{$H+}
> 
> interface
> 
> uses
>   Classes, SysUtils, unit1;
> 
> Type
>  TTest = class
>   a: byte;
>   b: byte;
>   function test(c,d: integer; MainForm: TForm1): integer;
>  end;
> 
> implementation
> 
> function TTest.test(c,d: integer; MainForm: TForm1): integer;
> begin
>  Result:=c+d+MainForm.x+MainForm.y;
> end;
> 
> end.
> ____________________
> 
> 
> I changed the 3rd unit, so now it returns:
> 
> unit3.pas(8,27) Fatal: Circular unit reference between Unit3 and Unit1
> 
> Thanks!
> Kjow
> 
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
> 


-- 
John vd Waeter
www.jvdw.nl
www.gps-carpool.net
www.shotinthedark.nl
www.pdaforms.nl
www.dbapocket.nl

john at jvdw.nl




More information about the Lazarus mailing list