Thema: Delphi Verkürzung des Codes

Einzelnen Beitrag anzeigen

Benutzerbild von Hador
Hador

Registriert seit: 11. Dez 2004
Ort: Recke
682 Beiträge
 
Turbo Delphi für Win32
 
#3

Re: Verkürzung des Codes

  Alt 8. Nov 2006, 21:35
Ich habs mir nur ganz kurz angeguckt, aber hab schon was zu meckern

Das allerwichtigste was du verbessern solltest, ist die Form deines Quellcodes. Das Stichwort heißt: Strukturieren!
Denn stelldir mal vor was passiert, wenn du etwas größere Programme schreibst und du dort immer so schreibst:
Delphi-Quellcode:
if (p1=p2)and (p1=p2)and(p1=p3)and(p1=p4)and(p1=p5)and(p1=p6)and(p1=p7)and(p1=p8)and(p1=p9)then
begin
label1.caption:='HERZLICHEN GLÜCKWUNSCH!!! SIE HABEN GEWONNEN!!!';
Neustart.Visible:=false;Panel1.Visible:=false;Panel2.Visible:=false;Panel3.Visible:=false;Panel4.Visible:=false;Panel5.Visible:=false;Panel6.Visible:=false;Panel7.Visible:=false;Panel8.Visible:=false;Panel9.Visible:=false;
neustart.Visible:=false;
Button1.Visible:=false;
 end;
Immer nur ein Befehl pro Zeile und zwischen begin/end einrücken ((p1=p2)and (p1=p2) ist wie dir vmtl. auch selbst klar ist, unsinn - und neustart.Visible:=false; hast du auch mehrmals drin):
Delphi-Quellcode:
if (p1=p2) and (p1=p3) and (p1=p4) and (p1=p5) and (p1=p6) and (p1=p7) and (p1=p8) and (p1=p9) then
begin
  label1.caption := 'HERZLICHEN GLÜCKWUNSCH!!! SIE HABEN GEWONNEN!!!';
  Neustart.Visible := False;
  Panel1.Visible := False;
  Panel2.Visible := False;
  Panel3.Visible := False;
  Panel4.Visible := False;
  Panel5.Visible := False;
  Panel6.Visible := False;
  Panel7.Visible := False;
  Panel8.Visible := False;
  Panel9.Visible := False;
  Button1.Visible:=false;
end;
Dann würde ich die Integer Variablen (p1,p2,p3,p4,p5,p6,p7,p8,p9:integer in einem Array speichern:
p: Array[1..9] of Integer; Darauf zugreifen kannst du dann mit P[1] bis P[9].

Wenn du dann deine 9 Panels ebenfalls noch in einem Array speicherst, dann kannst du deine berechnen-Prozedur bspw. folgendermaßen verkürzen:
Delphi-Quellcode:
for i := 1 to 9 do
  case p[i] of
    1: panel[i].color := clblack;
    2: panel[i].color := clred;
    3: panel[i].color := clgreen;
    0: panel[i].color := clyellow;
  end;
Sind jetzt nur ein paar Sachen, habs mir aber wie gesagt auch nur ganz kurz angeschaut

Aso und statt
Delphi-Quellcode:
if (p1=p2) and (p1=p3) and (p1=p4) and (p1=p5) and (p1=p6) and (p1=p7) and (p1=p8) and (p1=p9) then
begin
  label1.caption := 'HERZLICHEN GLÜCKWUNSCH!!! SIE HABEN GEWONNEN!!!';
  Neustart.Visible := False;
  Panel1.Visible := False;
  Panel2.Visible := False;
  Panel3.Visible := False;
  Panel4.Visible := False;
  Panel5.Visible := False;
  Panel6.Visible := False;
  Panel7.Visible := False;
  Panel8.Visible := False;
  Panel9.Visible := False;
  Button1.Visible:=false;
end;
kannst du dann natürlich auch
Delphi-Quellcode:
if (p1=p2) and (p1=p3) and (p1=p4) and (p1=p5) and (p1=p6) and (p1=p7) and (p1=p8) and (p1=p9) then
begin
  label1.caption := 'HERZLICHEN GLÜCKWUNSCH!!! SIE HABEN GEWONNEN!!!';
  Neustart.Visible := False;
  for i := 1 to 9 do
    Panel[i].Visible := False;
  Button1.Visible:=false;
end;
Lars Kiesow
http://www.larskiesow.de

Computer gehorchen deinen Befehlen, nicht deinen Absichten.
  Mit Zitat antworten Zitat