How to inherit a common virtual method?

I have the following code. I want to override the Notify method of the base base list so that I can create an event when the list changes.

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 Value: TSomeClass , I get a "Notify" declaration that is different from the previous announcement .

Notify is the protected TObjectList<T: class> method. This method does not appear in the main list of the XE2 IDE.

To somehow realize this, or do I need to use a different approach, since this is the notorious brick wall?

+5
source share
1 answer

If your descendant class corrects the generic type, then you need to use this fixed type instead of T. In your case:

 protected procedure Notify(const Value: TSomeclass; Action: TCollectionNotification); override; 

is the correct way to declare this function.


Error:

Notify ad differs from previous ad

It is an unfortunate case of duplicate Delphi RTL name types in different units.

System.Classes unit defines

 TCollectionNotification = (cnAdded, cnExtracting, cnDeleting); 

and System.Generics.Collections defines

 TCollectionNotification = (cnAdded, cnRemoved, cnExtracted); 

You almost certainly have Generics.Collections declared before Classes in your uses , and the compiler resolves the unwanted version of TCollectionNotification .

To fix this, either reorganize your uses clauses so that Generics.Collections after Classes or use the full type name, that is:

  procedure Notify(const Value: TSomeClass; Action: Generics.Collections.TCollectionNotification); override; 

The lesson with the differs from previous declaration error is a methodological check of your types. Ctrl + CLICK in the type identifier will lead you to determine the type used by the compiler.

+11
source

All Articles