[Lazarus] OOP basics - 2

Michael Van Canneyt michael at freepascal.org
Wed Apr 14 15:41:51 CEST 2010



On Wed, 14 Apr 2010, Kjow wrote:

> I'm sorry for the first email, it was a bit confusing.
>
> Now, I created a project to make more clear what I mean. Take it as a
> stupid example, I need this to make more "advanced" things:
>
> ____________________
>
> unit Unit1;
>
> {$mode objfpc}{$H+}
>
> interface
>
> uses
>  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
>  unit2, 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
>
> {$R *.lfm}
>
> { TForm1 }
>
> procedure TForm1.FormCreate(Sender: TObject);
> begin
>  init(1,2);
>  ShowMessage('Result: '+IntToStr(tester1.test(x,y)));
> 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.y:=tester2.test(a,-b);
> end;
>
> end.
>
> ____________________
>
> unit Unit3;
>
> {$mode objfpc}{$H+}
>
> interface
>
> uses
>  Classes, SysUtils;
>
> Type
> TTest = class
>  a: byte;
>  b: byte;
>  function test(x,y: integer): integer;
> end;
>
> implementation
>
> function TTest.test(x,y: integer): integer;
> begin
> Result:=x+y;
> end;
>
> end.
>
> ____________________
>
> Obviously I can't declare unit1 on unit2's "uses", because:
>
> unit2.pas(8,27) Fatal: Circular unit reference between Unit2 and Unit1

Put unit2 in the implementation uses list of unit1. Then it should work.

Michael.




More information about the Lazarus mailing list