Hallo zusammen,
ich habe folgende Klassen erstellt :
Delphi-Quellcode:
type
TModifyActionList = (lnNone, lnAdded, lnChange, lnDelete);
TDegree = class
private
FOnChange : TNotifyEvent;
FOnDelete : TNotifyEvent;
FModifyAction : TModifyActionList;
FID : Integer;
FDegree : string;
procedure SetID(const Value: Integer);
procedure SetDegree(const Value: string);
public
property ID : Integer read FID write SetID;
property Degree : string read FDegree write SetDegree;
property ModifyAction : TModifyActionList read FModifyAction write FModifyAction;
property OnChange : TNotifyEvent read FOnChange write FOnChange;
property OnDelete : TNotifyEvent read FOnDelete write FOnDelete;
end;
TCustomObjectList<T: class> = class(TObjectList<T>)
private
FOnChange : TNotifyEvent;
FOnDelete : TNotifyEvent;
FModifyAction : TModifyActionList;
FNewRecord : Boolean;
procedure DeleteRecord(Klasse: T);
protected
function getItem(Index: Integer): T; virtual;
procedure setItem(Index: Integer; Objekt: T); virtual;
procedure Notify(const Item: T; Action: TCollectionNotification); override;
public
function Add(Objekt: T): Integer; virtual;
function NewRecord(Objekt: T): Integer; virtual;
function Remove(Objekt: T): Integer; virtual;
function IndexOf(Objekt: T): Integer; virtual;
procedure Insert(Index: Integer; Objekt: T); virtual;
property Items[index: Integer]: T read getItem write setItem; default;
property ModifyAction : TModifyActionList read FModifyAction write FModifyAction;
property OnChange : TNotifyEvent read FOnChange write FOnChange;
property OnDelete : TNotifyEvent read FOnDelete write FOnDelete;
end;
{...}
{ TDegree }
procedure TDegree.SetDegree(const Value: string);
begin
FDegree := Value;
if (Assigned(FOnChange)) and (FModifyAction = lnChange) then
FOnChange(Self);
end;
procedure TDegree.SetID(const Value: Integer);
begin
FID := Value;
if (Assigned(FOnChange)) and (FModifyAction = lnChange) then
FOnChange(Self);
end;
{ TCustomObjectList<T> }
function TCustomObjectList<T>.Add(Objekt: T): Integer;
begin
FNewRecord := False;
Result := inherited Add(Objekt);
end;
procedure TCustomObjectList<T>.DeleteRecord(Klasse: T);
begin
// To do something
end;
function TCustomObjectList<T>.getItem(Index: Integer): T;
begin
Result := T(inherited Items[Index]);
end;
function TCustomObjectList<T>.IndexOf(Objekt: T): Integer;
begin
Result := inherited IndexOf(Objekt);
end;
procedure TCustomObjectList<T>.Insert(Index: Integer; Objekt: T);
begin
inherited Insert(Index, Objekt);
end;
function TCustomObjectList<T>.NewRecord(Objekt: T): Integer;
begin
FNewRecord := True;
Result := inherited Add(Objekt);
end;
procedure TCustomObjectList<T>.Notify(const Item: T;
Action: TCollectionNotification);
begin
if (Assigned(FOnChange)) and (FNewRecord) and (Action <> cnRemoved) then
begin
FNewRecord := False;
FOnChange(Self);
end;
if Action = cnRemoved then
begin
FOnDelete(Item);
end;
inherited Notify(Item, Action);
end;
function TCustomObjectList<T>.Remove(Objekt: T): Integer;
begin
Result := inherited Remove(Objekt);
end;
procedure TCustomObjectList<T>.setItem(Index: Integer; Objekt: T);
begin
inherited Items[Index] := Objekt;
end;
Das funktioniert auch alles soweit ganz gut, bis auf den Teil mit FOnDelete. Warum bekomme ich jedesmal bei der Zeile
FOnDelete(Item);
eine AccessViolation ? Wie kann ich das beheben und was habe ich falsch gemacht ?