Autor Beitrag
olliistmeinname
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 19.06.03 12:57 
Hi !

Bei mir soll eine Variable den Wert beibehalten , wenn die Procedure beendet ist . Wenn sie wieder startet soll der Wert weiter benutzt werden.



ausblenden Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.Button1Click(Sender: TObject);
VAR variable : integer;
begin
variable := +1;
edit1.text := inttostr(variable);
end;


Wenn ich auf den Button1 4mal klicke , dann soll in edit1 die zahl 4 stehen.


mfg OLLI

_________________
Olliistmeinname und bin da für jede Dame
Tweafis
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 647

WinXP + fbsd
Delphi 5 Prof
BeitragVerfasst: Do 19.06.03 13:03 
entweder du benutzt globale variablen oder private

_________________
.: Es wird der Tag kommen, an dem wir es nicht mehr ändern können :.
FriFra
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 557

Win XP Prof, Win XP Home,Win Server 2003,Win 98SE,Win 2000,Win NT4,Win 3.11,Suse Linux 7.3 Prof,Suse Linux 8.0 Prof
D2k5 Prof, D7 Prof, D5 Standard, D3 Prof, K3 Prof
BeitragVerfasst: Do 19.06.03 13:04 
Dafür darfst Du die Variable nicht in der Procedure deklarieren, da diese nach deren Ablauf nicht mehr existiert. Du musst die Variable global (oberhalb von implementation) deklarieren.

_________________
Michael
(principal certified lotus professional - developer)
olliistmeinname Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 19.06.03 13:56 
ausblenden volle Höhe 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:
35:
36:
unit Unit1;

interface

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

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

  end;

var
  Form1: TForm1;
  variable :integer;


implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin

variable := +1;
edit1.text := inttostr(variable);
end;

end.



So geht es nicht . Im Editfeld ist immer eine '1' zu sehen.

_________________
Olliistmeinname und bin da für jede Dame
Tweafis
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 647

WinXP + fbsd
Delphi 5 Prof
BeitragVerfasst: Do 19.06.03 14:11 
variable := +1;ersetzen durchinc(variable);

_________________
.: Es wird der Tag kommen, an dem wir es nicht mehr ändern können :.
olliistmeinname Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 19.06.03 14:20 
THX @ALL

_________________
Olliistmeinname und bin da für jede Dame
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Do 19.06.03 14:37 
Tweafis hat folgendes geschrieben:
variable := +1;ersetzen durchinc(variable);

Oder durch
ausblenden Delphi-Quelltext
1:
variable := variable + 1;					

_________________
Ist Zeit wirklich Geld?
Tweafis
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 647

WinXP + fbsd
Delphi 5 Prof
BeitragVerfasst: Do 19.06.03 14:57 
Zitat:
X wird um 1 inkrementiert oder um N, wenn N angegeben ist. Das bedeutet, Inc(X) entspricht der
Anweisung X := X + 1, und Inc(X, N) ist mit der Anweisung X := X + N identisch. Inc generiert jedoch hochoptimierten Maschinencode und ist besonders für Schleifen geeignet.


:mrgreen: :mrgreen: :mrgreen:

_________________
.: Es wird der Tag kommen, an dem wir es nicht mehr ändern können :.


Zuletzt bearbeitet von Tweafis am Do 19.06.03 14:58, insgesamt 1-mal bearbeitet
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 19.06.03 14:57 
Und deklarier die Variable bitte im Private oder Public Abschnitt deiner Form-Klasse. Wir wollen ja OOP komform programmieren. :wink:
CenBells
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1547

Win 7
Delphi XE5 Pro
BeitragVerfasst: Do 19.06.03 16:43 
was mir ein kumpel letztens gezeigt hat,

füge mal folgendes in den public abschnitt ein

procedure meinTest;

und unter implementation

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.meinTest;
const
  LVariable: Integer = 0;
begin
   inc(LVariable);
  Edit1.Text := IntToStr(LVariable);
end;


dann noch in der procedure Button1Click nr meinTest schreiben et voilá.

Gruß
Ken
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 19.06.03 16:48 
Ist aber ein unschöner Trick, in dem das Schlüsselwort const mißbraucht wird. const drück ja aus, dass es sich um eine Konstante handelt und die Eigenschaft einer Konstante manifestiert sich ja schon im Namen, sie sollte konstant bzw. unveränderlich sein. Nicht umsonst wird ja die Unterteilung in var und const gemacht.

Meine Meinung.
FriFra
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 557

Win XP Prof, Win XP Home,Win Server 2003,Win 98SE,Win 2000,Win NT4,Win 3.11,Suse Linux 7.3 Prof,Suse Linux 8.0 Prof
D2k5 Prof, D7 Prof, D5 Standard, D3 Prof, K3 Prof
BeitragVerfasst: Do 19.06.03 16:50 
CenBells hat folgendes geschrieben:
was mir ein kumpel letztens gezeigt hat,

füge mal folgendes in den public abschnitt ein

procedure meinTest;

und unter implementation

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.meinTest;
const
  LVariable: Integer = 0;
begin
   inc(LVariable);
  Edit1.Text := IntToStr(LVariable);
end;


dann noch in der procedure Button1Click nr meinTest schreiben et voilá.

Gruß
Ken


:autsch: :autsch: :autsch: :autsch: :autsch: :autsch: :autsch: :autsch:

_________________
Michael
(principal certified lotus professional - developer)
Tweafis
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 647

WinXP + fbsd
Delphi 5 Prof
BeitragVerfasst: Do 19.06.03 16:51 
Soweit ich weiß kann man

a) Konstanten nach dem Definieren nicht merh verändern
b) dürfte das immer 1 geben

// Edit: Oh hab mich wohl geirrt. Aber wenn das geht, ist es nicht gerade der richtige weg.

_________________
.: Es wird der Tag kommen, an dem wir es nicht mehr ändern können :.


Zuletzt bearbeitet von Tweafis am Do 19.06.03 16:54, insgesamt 2-mal bearbeitet
CenBells
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1547

Win 7
Delphi XE5 Pro
BeitragVerfasst: Do 19.06.03 16:51 
@luckie, ich finde es auch nicht schön, allerdings hatte ich mit nem kumpel ne wette laufen und ich war der meinung, daß der speicher einer constanten ebenfalls bei jedem prozeduraufruf neu alloziiert wird und dadurch die werte ebenfalls, wie bei variablen verloren gehen.. Naja, er hat die wette gewonnen *G*

@Tweafis: Nein, man kann Konstanten auch noch ändern, wenn sie typisiert sind. Und das gibt nicht immer eins...
[edit]sieht vielleicht nicht gut aus, hat aber gefunzt[/edit]

@FirFra *G*G* :nut: :nut: :nut: :nut: :nut: :nut:

Gruß
Ken
CenBells
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1547

Win 7
Delphi XE5 Pro
BeitragVerfasst: Do 19.06.03 16:59 
hallo, ich nochmal,

nachdem mein obiger post solche wahren begeisterungsstürme *G* hervorgerufen hat, würde ich gerne mal wissen, ob wir hier nicht mal im offtopic so ne Art Freak-Show eröffnen wollen? Dort kommt dann alles hinein, was entweder einfach nur buggy oder äusserst unnormal ist/war und hier als code geschrieben wurde.
In der firma, in der ich arbeite, gibt es monatlich immer ein paar mails, wo dann die bugs "angeprangert" werden. Soll heißen, einer schneidet das stückchen aus und schickt es rum, mit der erklärung, was falsch war, oder sonstigen schnacks...

Gruß
Ken