I have the following code. I want to override the Notify method of the base base list on this to be able to create an event on the modification of the list.
TDescendantList = class(TObjectList<TSomeclass>)
private
<...>
protected
procedure Notify(const Value: T;
Action: TCollectionNotification); override;
<...>
end;
If I put Value: T
I get an "Undeclared identifier" on T.
If is Value: TSomeClass
I get the "Declaration of 'Notify' differs from previous declaration".
Notify
is a protected method of TObjectList<T: class>
. This method does not appear on overriding list of the XE2 IDE.
That's some way to implement this or I need to use another approach as this is an proverbial brick wall?
If your descendant class is fixing the generic type then you have to use that fixed type in place of T. In your case :
protected
procedure Notify(const Value: TSomeclass;
Action: TCollectionNotification); override;
is the correct way to declare this function.
The error :
Declaration of 'Notify' differs from previous declaration
is a regrettable case of the Delphi RTL duplicating type names in different units.
The unit System.Classes
defines
TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
and System.Generics.Collections
defines
TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);
Almost certainly you have Generics.Collections
declared before Classes
in your uses
clause and the compiler is resolving the undesired version of TCollectionNotification
.
To fix it, either reorganize your uses
clauses so that Generics.Collections
comes after Classes
or use a fully qualified type name, ie :
procedure Notify(const Value: TSomeClass;
Action: Generics.Collections.TCollectionNotification); override;
The lesson with a differs from previous declaration
error is to methodically check your types. Ctrl+CLICK on the type identifier will take you to the definition of the type the compiler is using.