Autor Beitrag
maniac_89
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Sa 26.02.05 11:17 
Hi,
ich mal fragen wie man eine Funktion aus einer anderen Unit aufrufen kann.
Zum Beispiel:

ausblenden volle Höhe Delphi-Quelltext
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:
unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
 //=> hier soll jetzt die Funktion aufgerufen werden.
end;

end.


ausblenden volle Höhe Delphi-Quelltext
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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;

     private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;



var
  Form1: TForm1;

implementation

{$R *.dfm}

function einlesen(zwert: integer):integer;  //=> das ist die Funktion
begin
 zwert := strtoint (form1.Edit1.text);
 einlesen := zwert;
end;

end.



Ich hoffe ihr könnt mir helfen.


Steffen, Maniac_89

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.
Moderiert von user profile iconUGrohne: Topic aus Sonstiges verschoben am Sa 26.02.2005 um 18:51
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Sa 26.02.05 11:19 
Du machst einfach ein "uses dieAndereUnit;" rein.
maniac_89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Sa 26.02.05 16:24 
Und wo, kannst du mir das nicht in den Quelltext reinschreiben???
OneOfTen
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 26.02.05 16:35 
such doch in deinem quelltext einfach mal das stichwort "uses", die unit einfach mit komma abtrennen :wink:
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Sa 26.02.05 16:36 
Nach dem implementation.

//Edit: Wenn es im interface ist, dann können alle auf die Unit2 zugreifen, die auch auf die Unit1 zugreifen. Je nach dem was du brauchst.
maniac_89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Sa 26.02.05 16:48 
Das klappt bei mir nicht.
OneOfTen
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 26.02.05 16:54 
was klappt nicht?

machs mal so: du schreibst dort, wo du deine Funktion aufrufst:
ausblenden Delphi-Quelltext
1:
Form2.NamederFunktion; // Aufruf der Funktion					


dann wirst du beim Compilieren automatisch gefragt, ob du Unit 2 in deine aufnehmen willst und er macht das automatisch..
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Sa 26.02.05 16:55 
Gib der Unit mal nen Namen und speichere sie mal ab.
maniac_89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Sa 26.02.05 17:57 
Könnt ihr nicht mal den kompelten Queltext eingeben
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Sa 26.02.05 18:19 
ausblenden volle Höhe Delphi-Quelltext
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:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls{, unit1};

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

Uses unit1;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  einlesen(10);
end;

end.
ausblenden volle Höhe Delphi-Quelltext
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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;

     private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;



var
  Form1: TForm1;

function einlesen(zwert: integer):integer; //Über dem Impementation deklarieren, damit sie bekannt ist

implementation

{$R *.dfm}

function einlesen(zwert: integer):integer;  //=> das ist die Funktion
begin
 zwert := strtoint (form1.Edit1.text);
 einlesen := zwert;
end;
Wenn du die Funktion als Teil der Klasse TForm1 deklarierst, dann brauchst du sie über dem implementation Teil nicht deklarieren.

Gruß Hape!

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
maniac_89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Sa 26.02.05 22:12 
Funktioniert leider immer noch nicht es kommt immer die Fehlermeldung:

'Deklaration erwartet, aber 'USES' gefunden'.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Sa 26.02.05 22:41 
So schwer kann das nun definitiv nicht sein...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
unit Unit1;
interface
uses Forms{, ...};
Type TForm1 = class(TForm)
 procedure Button1Click(Sender: TObject);
end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
Uses Unit2;
procedure TForm1.Button1Click(Sender: TObject);
begin
 RufMichAuf;
end;
end.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
unit Unit2;
interface
 procedure RufMichAuf;
implementation
 procedure RufMichAuf;
 begin
  // ...
 end;

end.
maniac_89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Sa 26.02.05 23:22 
Geht leider immer noch nicht, ich kann auch, nichts dafür bin eben noch Anfänger :oops: .
Es kommt immer diese Fehlermeldung:

[Fehler] Unit2.pas(11): Ungenügende Forward- oder External-Deklaration: 'TForm2.FormCreate'
[Fataler Fehler] Unit1.pas(25): Verwendete Unit 'Unit2.pas' kann nicht compiliert werden
OneOfTen
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 27.02.05 07:21 
poste doch noch mal beide units (also quelltexte), wo genau diese fehler auftauchen, dann können wir ihn vielleicht finden