Autor Beitrag
M.Mü
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Di 22.07.08 19:30 
Kann man dieses Problem umgehen, ohne alles in eine Unit zu packen???

Leider brauchen, schon beide Interfaces der Units sich gegenseitig. Wenn ich eine nur im Implementation-Teil bräuchte, ginge es ja :(

Dabei fällt mir noch ein ... kann man mehrere Formulare in eine Unit packen??? ... ja gut, wenn man ohne Drag&Drop programmiert sicherlich. Aber mit diesem Luxus?

VG M.Mü
huuuuuh
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 665
Erhaltene Danke: 19

win xp, (win vista), win 7
VS 2008 Express Edition, VS 2010 Express Edition, VS 2010 Professionell
BeitragVerfasst: Di 22.07.08 19:44 
delphi erstellt für jede form eine eigene unit. mehrere forms in einer unit kannst du also vergessen.
Timosch
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1314

Debian Squeeze, Win 7 Prof.
D7 Pers
BeitragVerfasst: Di 22.07.08 21:09 
user profile iconM.Mü hat folgendes geschrieben:
Kann man dieses Problem umgehen, ohne alles in eine Unit zu packen???

Die sich zirkulär referenzierenden Units über die uses-Klausel im implementation-Bereich einbinden.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
interface
uses Dialogs, Windows,...
...
implementation
uses unit2;
...
end.

_________________
If liberty means anything at all, it means the right to tell people what they do not want to hear. - George Orwell
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 22.07.08 21:22 
Beides im Interface-Teil geht nicht ohne zirkuläre Referenz. Die Frage ist, wieso du die Referenz im Interface-Teil brauchst. Was du tun kannst ist eine allgemeine TForm Referenz als Feld im Interface-Teil zu haben und im Implementation Teil zu casten.

Mit Class-Helper kommst du aber einigermassen Nahe an dein Ziel ran. Das ist aber reine Compiler-Trickserei und kann im Prinzip nichts, was du nicht auch über globale Funktionen erreichen könntest. Es ist reine Code-Kosmetik aber schlussendlich sieht es tatsächlich aus, als ob beide Klassen die gewünschten Kreuzreferenzen besitzen. Jedenfalls kannst du den Class Helper im Implementation-Teil einfügen und kannst so künstlich eine neue Property in TForm1/TForm2 einfügen, welche die gewünschte Referenz auf die jeweils andere Form castet. Hab unten mal ein Beispiel eingefügt, jedoch ohne den Cast sondern der Einfachheit halber mit statischer Verlinkung.

ausblenden UForm1.pas
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
unit UForm1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(Self.Form2.Caption); // ("Self" is just for clarity of example)
end;

end.

ausblenden UForm2.pas
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
unit UForm2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  ShowMessage(Self.Form1.Caption); // ("Self" is just for clarity of example)
end;

end.

ausblenden volle Höhe Unit1.pas
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
unit Unit1;

interface

uses UForm1, UForm2;

type
  TForm1H = class helper for TForm1
  private
    function GetForm2: TForm2; inline;
  public
    property Form2: TForm2 read GetForm2;
  end;

type
  TForm2H = class helper for TForm2
  private
    function GetForm1: TForm1; inline;
  public
    property Form1: TForm1 read GetForm1;
  end;

implementation

{ TForm1H }

function TForm1H.GetForm2: TForm2;
begin
  // alternatively store TForm reference to Form2 in TForm1 and cast to TForm2 here
  Result := UForm2.Form2;
end;

{ TForm2H }

function TForm2H.GetForm1: TForm1;
begin
    // alternatively store TForm reference to Form1 in TForm2 and cast to TForm1 here
  Result := UForm1.Form1;
end;

end.
M.Mü Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Di 22.07.08 21:29 
Hallo,

hab jetzt doch alles in eine Unit gepackt ...

Ein Formular hatte ich lediglich als Muster, d.h. im Grunde nur eine erweiterte Klasse von TForm. Wenn ich jetzt ein neues Formular erstelle, mache ich es mit Delphi und ersetze dann TForm mit meiner eigenen. Zwar net ganz so schön aber es funktioniert.

Habt Dank.

M.Mü