Autor Beitrag
rockminstrel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38

Win XP Prof.
Delphi 5 Prof., Delphi 2005 pers., Turbo Delphi 2006 Exp.
BeitragVerfasst: Mi 24.01.07 10:01 
Hallo,
ich bin neu hier im Forum und programmiere schon seit einigen Jahren sporadisch mit Delphi. Da ich noch nie ein Buch gelesen habe und folglich mein gesamtes Wissen auf learning by doing basiert ist es auch recht verstreut. Es kann also sein, dass ich zwar mit "normalen" Variablen umgehen kann aber z.B. keine Ahnung von Pointern habe.(Daher wahrscheinlich auch meine Schwierigkeiten)

Nun zu meinem Problem: Ich bin dabei ein Dienstzeitblatt für meine Zivildienststelle zu programmieren (ich könnte es zwar auch mit Excel machen, aber ich brauche was zu tun :D). Das Programm erstellt bisher die benötigten Editfelder und man kann über eine Monats- und Jahresangabe ihre Anzahl verändern.

Das Problem liegt darin, dass wenn ich das Programm nach der Erstellung der Edits verlasse entweder ein EInvalidPointer (beim Kompilieren) oder ein Runtime Error 216 (Normalstart). Wenn ich das Programm beende, bevor ich die Edits erstellt habe kommt dieser Fehler nicht. Ich habe auch schon die Suche benutzt habe aber leider keine passende Lösung gefunden, da ich z.B. keine onclose-Prozdur habe.

Hier mal der Quelltext:

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:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
type
  TForm1 = class(TForm)
    Label1: TLabel;
    MainMenu1: TMainMenu;
    Datei1: TMenuItem;
    Neu1: TMenuItem;
    Beenden1: TMenuItem;
    Procedure edits;
    procedure Neu1Click(Sender: TObject);
    procedure Menge;
  private
     
     { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  Daten: array of real;
  EditsArray: Array of TEdit;
  TZahl: integer;
implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.edits;
var
  i,r,z : Integer;
begin
Menge;     
z:=1;
  SetLength(EditsArray, Tzahl*10);
  SetLength(Daten, Tzahl*10);
  for r:=1 to TZahl do
      begin
      for i:=1 to 10 do
          begin
          EditsArray[z] := TEdit.Create(self);
          with EditsArray[z] do
                begin
                parent := self;
                width:=20;
                left := i * width + 5;
                top := r * 25;
                name := 'edit'+IntToStr(z);
                text := '5';
                Daten[i]:=strtofloat(text);
                z:=z+1;
                end;
          end;
      end;
label1.caption:=floattostr(Daten[2]);
end;

procedure Tform1.Menge;
var t:integer;
begin
case Jahr.m of
1:TZahl:=31;
2:     begin
       TZahl:=28;
       for t:=0 to 100 do
       if Jahr.j=2007+4*t then Tzahl:=29;
       end;
3:TZahl:=31;
4:TZahl:=30;
5:TZahl:=31;
6:TZahl:=30;
7:TZahl:=31;
8:TZahl:=31;
9:TZahl:=30;
10:TZahl:=31;
11:TZahl:=30;
12:TZahl:=31;
end;
label1.caption:=inttostr(TZahl);
end;


Ich hoffe mir kann jemand helfen :(
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Mi 24.01.07 11:03 
Denke bitte bei Array of sowieso daran, dass der Index des Array grundsätzlich bei 0 beginnt und bis Länge-1 geht. Also Deine Indexvariable z sollte mit 0 initialisiert werden.

_________________
Es gibt keine Probleme - nur Lösungen!
rockminstrel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38

Win XP Prof.
Delphi 5 Prof., Delphi 2005 pers., Turbo Delphi 2006 Exp.
BeitragVerfasst: Mi 24.01.07 11:07 
Danke hat hervorragend funktioniert. :D Ich wollte aber auch gerade posten, dass ich das Problem gelöst habe, indem ich setlenght von Editsarray auf TZahl*11 geändert habe. Ich weiss aber leider nicht warum es funktioniert. :?!?:
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 25.01.07 15:59 
Das SetLength funktioniert, weil du dadurch keine Bereichsüberschreitung mehr in Zeile 42 erzeugst. Das kannst Du testen, indem Du in den Compiler-Optionen für das Projekt die Bereichsprüfung aktivierst.

Die Änderung von Zeile 35 und 36 von TZahl*10 auf TZahl*11 behebt zwar das Problem nicht aber die Ursache (die wie bereits erwähnt) damit zusammenhängt, dass Du Arrays ab 1 indizierst, dynamische Arrays aber immer bei 0 beginnen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
rockminstrel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38

Win XP Prof.
Delphi 5 Prof., Delphi 2005 pers., Turbo Delphi 2006 Exp.
BeitragVerfasst: Do 25.01.07 16:40 
Vielen Dank, jetzt bin ich schlauer. :D :D