Autor Beitrag
majolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 334

Ubuntu 8.04, WinXP Prof.
D1, D6Pers, D7 Prof., D8 Prof., D2005 Pers
BeitragVerfasst: Fr 04.10.02 23:00 
Hi,

ist es möglich, die Variablen die in einer Procedure verwendet in einer zweiten zu verwenden?
Bsp:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.Procedure1;
var i:integer; s:string;
begin
s:= Inhalt1;
i:=534;
end;

procedure Tform1.Procedure2;
begin
Möchte inhalt der Variablen aus Procedure1;
end;


Nur ein Beispiel.Danke im vorraus.
Gruss
majolo
Manfred
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 90



BeitragVerfasst: Fr 04.10.02 23:39 
Hi!
Drei Möglichkeiten:
1.: Deklaration der Variablen nicht in der Prozedur sondern global vornehmen:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var i:integer; s:string; 

procedure TForm1.Procedure1; 
begin 
s:= Inhalt1; 
i:=534; 
end; 

procedure Tform1.Procedure2; 
begin 
Ich kann i und s beliebig nutzen
end;


2.: Variable als Parameter überreichen:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.Procedure1; 
var i:integer; s:string; 
begin 
s:= Inhalt1; 
i:=534; 
procedure2 ( i , s ) ;
end; 

procedure Tform1.Procedure2 (i:integer; s:string); 
begin 
Auch hier habe ich i und s zur Verfügung
end;


3.: Pointer verwenden
Prozedur1 muss hierzu einen global zur Verfügung stehenden Pointer mit der Adresse einer Variablen bestücken, z.B. so:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
var
  p : TPointer ;

procedure TForm1.Procedure1; 
var i:integer; s:string; 
begin 
p := @i ;
s:= Inhalt1; 
i:=534; 
end; 

procedure Tform1.Procedure2; 
begin 
Möchte inhalt der Variablen aus Procedure1; 
In p^ steht jetzt der Wert von i zur Verfügung
end;

_________________
Computer können schneller rechnen als wir, deshalb machen sie auch mehr Fehler
aogwaba
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109



BeitragVerfasst: Sa 05.10.02 10:07 
Hi!

Nein, ist nicht möglich. Lokale Variablen werden beim verlassen der procedure gelöscht.
Du kannst der Procedure aber Variablen übergeben:

Call by Value :
procedure myProc(i:integer);
i ist eine Kopie.

Call by Referenz :
procedure myProc(var i:integer);
i ist eigentlich ein Zeiger.

oder du benutzt Functionen oder globale Variablen :(

cu
waba
majolo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 334

Ubuntu 8.04, WinXP Prof.
D1, D6Pers, D7 Prof., D8 Prof., D2005 Pers
BeitragVerfasst: Sa 05.10.02 11:41 
Das mit dem global wusste ich,wollte ich aber nicht so deklarieren.
Sondern einfach so etwas wie Manfred vorgeschlagen hat.Danke werde es gleich mal ausprobieren.Functionen würden in meinem Fall leider nichts bringen.Schade!Danke.
Gruss
majolo
majolo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 334

Ubuntu 8.04, WinXP Prof.
D1, D6Pers, D7 Prof., D8 Prof., D2005 Pers
BeitragVerfasst: Sa 05.10.02 20:19 
@Manfred: Könnte es sein,dass im Zweiten Bsp ein Fehler ist?Bei mir funzt der Code nicht.
Gruss
majolo
majolo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 334

Ubuntu 8.04, WinXP Prof.
D1, D6Pers, D7 Prof., D8 Prof., D2005 Pers
BeitragVerfasst: Sa 05.10.02 21:55 
Problem hat sich erledigt.Ist kein Fehler drin.Nach Neustart war der Fehler beseitigt.
Gruss
majolo
Wolff68
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 302
Erhaltene Danke: 1

WinXP home
D6 Prof
BeitragVerfasst: So 06.10.02 17:52 
Ähmm. Also nur mal als Zusammenfassung:

Manfred schlägt vor den Pointer auf die Variable Global weiterzureichen.
aogwaba erklärt aber richtigerweise, daß die Variablen nach verlassen der Prozedur gelöscht werden !!!

Also ist der Inhalt der Speicheradresse auf die der Zeiger deutet nicht weiter gesichert. Je nach dem was zwischen Prozedur 1 und 2 für Variablen angelegt / Gelöscht / geschrieben wird kann sich der Inhalt der Speicheradresse ändern.

Also Vorsicht damit !!
Hatte wegen so 'nem Thema mit PChars schonmal heftige Probleme. Vor allem weil es 99x gut gehen kann und dann plötzlich hast was völlig unsinniges in der Variablen.

Grüße,
Wolfgang

_________________
"Der Mensch ist nicht was er sich vorstellt oder wünscht zu sein, sondern das was andere in ihm sehen."