Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - dynamischer array soll daten aus einem stringgrid übernehmen


MeistaBene - Mo 18.09.06 16:45
Titel: dynamischer array soll daten aus einem stringgrid übernehmen
moin,
habe folgendes kleines problem. versuche verzweifelt den inhalt eines stringgrid in einen array reinzubekommen, aber ich bekomme immer einen Fehler.
hier ist mein prox. lassen die felder erst ausfüllen, da ich das als fehlerquelle ausschließen wollte.
der user des prox soll erst die anzahl von messwerten auswählen und sie dann selber eingeben können. dies ist nur ein test warum es mit dem stringgrid und dem array nicht klappt.

mfg meistabene


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:
81:
82:
83:
84:
85:
86:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Button1: TButton;
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  daten : array of array of string;


implementation

{$R *.dfm}

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
case ComboBox1.ItemIndex of
    0: stringgrid1.ColCount := 1;
    1: stringgrid1.ColCount := 7;
    2: stringgrid1.ColCount := 9;
    3: stringgrid1.ColCount := 11;
    4: stringgrid1.ColCount := 13;
    5: stringgrid1.ColCount := 15;
    6: stringgrid1.ColCount := 17;
    7: stringgrid1.ColCount := 19;
    8: stringgrid1.ColCount := 21;

  end;
end;

procedure TForm1.ComboBox2Change(Sender: TObject);
begin
case ComboBox2.ItemIndex of
    0: stringgrid1.rowCount := 1;
    1: stringgrid1.rowCount := 2;
    2: stringgrid1.rowCount := 3;
    3: stringgrid1.rowCount := 4;
    4: stringgrid1.rowCount := 5;
    5: stringgrid1.rowCount := 6;
    6: stringgrid1.rowCount := 7;
    7: stringgrid1.rowCount := 8;
    8: stringgrid1.rowCount := 9;
    9: stringgrid1.rowCount := 10;
    10:stringgrid1.rowCount := 11;

  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i, j : integer;
begin
  for i := 0 to (stringgrid1.RowCount-1) do
   for j := 0 to (stringgrid1.ColCount-1) do
    begin
    stringgrid1.Cells[j,i] := inttostr(j)+inttostr(i);
    end;


 SetLength(daten,(stringgrid1.rowcount),(stringgrid1.ColCount));
  for i := 0 to (stringgrid1.RowCount) do
   for j := 0 to(stringgrid1.ColCount) do
    begin
    daten[j,i] := stringgrid1.Cells[j,i];
    end;

end;

end.


Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt


Marc. - Mo 18.09.06 16:49

Du verwendest ein dynamisches Array, soll heißen, bevor du Werte in dein Array speichern kannst, musst du den Speicher erst mit Setlength(Array, Länge); reservieren ;)


MeistaBene - Mo 18.09.06 17:03

SetLength(daten,(stringgrid1.rowcount),(stringgrid1.ColCount));


so in etwa kann das aussehen oder?? Das habe ich auch gemacht und da die länge von dem benutzer bestimmt werden kann habe ich die anzahl der spalten und zeilen eingebunden

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt


kotzekocher - Di 19.09.06 12:40

Ich glaube, du hast bei der Zuweisung

Delphi-Quelltext
1:
daten[j,i] := stringgrid1.Cells[j,i];                    

die Dimensionen vertauscht. Richtig muss es heißen

Delphi-Quelltext
1:
daten[i,j] := stringgrid1.Cells[i,j];                    

Es dürfte nur gehen, solange Zeilen und Spaltenanzahl gleich sind. Was kommt denn für ein Fehler?


MeistaBene - Di 19.09.06 13:51

Es ist ein EAccessViolation, zugriffsverletzung bei adresse ####### in modul project1. lesen bei adresse ########.

umgedreht habe ich auch schon und es hilft nicht. bin am verzweifeln


Lannes - Di 19.09.06 15:35

Hallo,

einmal nimmst Du Count - 1, dann wieder nur Count :!: , vermute daher die Zugriffsverletzung.

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TForm1.Button1Click(Sender: TObject);  
var i, j : integer;  
begin  
  for i := 0 to (stringgrid1.RowCount-1) do  
   for j := 0 to (stringgrid1.ColCount-1) do  
    begin  
    stringgrid1.Cells[j,i] := inttostr(j)+inttostr(i);  
    end;  

 SetLength(daten,(stringgrid1.rowcount),(stringgrid1.ColCount));  
  for i := 0 to (stringgrid1.RowCount) do  
   for j := 0 to(stringgrid1.ColCount) do  
    begin  
    daten[j,i] := stringgrid1.Cells[j,i];  
    end;   
end;