Die Methode Add funktioniert.
Aber bei GetItem und bei Destroy kommt ein SIGESV, wobei, wen die Liste keine Elemente hat, funktioniert auch Destroy.
Was noch interessant ist, bei Destroy, wird der SIGSEV auch bei GetItem ausgelöst. entferne ich aber die Self-Zeile dort, dannn kommt das Assembler Fenster.
Starte ich aber da ganze direkt von der Konsole, dann wird es ohne Fehler beendet.
Was noch ist, das SIGSEV, kommt nicht in der Result-Zeile, sondern bei "begin".
Ich denke, ich mache irgend etwas mit den Zeigern falsch, nur die Frage wo ?
Code: Alles auswählen
type
PColorString = ^TColorString;
TColorString = record
s: string;
col: TColor;
end;
{ TColorStringList }
TColorStringList = class(TList)
private
function GetItem(index: integer): TColorString;
procedure SetItem(index: integer; AValue: TColorString);
public
property Items[index: integer]: TColorString read GetItem write SetItem; default;
constructor Create;
destructor Destroy; override;
procedure Add(s: string; col: TColor = clGray);
end;
implementation
{ TColorStringList }
function TColorStringList.GetItem(index: integer): TColorString;
begin
Result := Self[index];
// Result.s:='fgfgfgd'; // Mit diesen 2 Zeilen geht es.
// Result.col:=clRed;
end;
procedure TColorStringList.SetItem(index: integer; AValue: TColorString);
begin
Self[index] := AValue;
end;
constructor TColorStringList.Create;
begin
inherited Create;
end;
destructor TColorStringList.Destroy;
var
cs: PColorString;
i: integer;
begin
for i := 0 to Self.Count - 1 do begin
cs^ := Self[i];
Dispose(cs);
end;
inherited Destroy;
end;
procedure TColorStringList.Add(s: string; col: TColor);
var
cs: PColorString;
begin
New(cs);
cs^.s := s;
cs^.col := col;
inherited Add(cs);
end;
Das HauptForm ist eigentlich nicht besonders:
Code: Alles auswählen
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
cs: TColorStringList;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
cs.Add('Hello World: ' + IntToStr(cs.Count), clRed);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Caption := cs.Items[2].s;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
cs := TColorStringList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
cs.Free;
end;