Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OnNotify of generic TList

I want to use the OnNotify event of a generic TList. Assigning a procedure to OnNotify yields the error message:

E2010 Incompatible types: 'System.Generics.Collections.TCollectionNotification' and 'System.Classes.TCollectionNotification'

I am declaring a class and in it a generic TList is used as follows:

TEditor_Table = class (TObject)
public
  FEditors: TList<TGradient_Editor>;  // List containing the editors

This is not the neatest way of doing it but I need this for a test. The list is instantiated in the constructor:

constructor TEditor_Table.Create (Owner: TFMXObject);
begin
   inherited Create;

   FEditors := TList<TGradient_Editor>.Create;
   FOwner := Owner;
end; // Create //

Next in the main form a function is declared

procedure do_editor_change (Sender: TObject; const Item: TGradient_Editor; Action: TCollectionNotification);

and the TColor_Editor class is instantiated as follows:

FColor_Editor := TEditor_Table.Create (List_Gradients);
FColor_Editor.FEditors.OnNotify := do_editor_change;
                                                   ^
error occurs here----------------------------------+

I do not understand the message at all and I am at a los why the compiler seems to confuse the two units: 'System.Generics.Collections.TCollectionNotification' and 'System.Classes.TCollectionNotification'. What am I doing wrong?

like image 728
Arnold Avatar asked Nov 07 '13 20:11

Arnold


1 Answers

The problem is that the RTL defines two different versions of TCollectionNotification. One in System.Classes and one in Generics.Collections.

You are using TList<T> from Generics.Collections and so need the TCollectionNotification from Generics.Collections. But in your code TCollectionNotification is the version declared in System.Classes. That's because, at the point where you write TCollectionNotification, System.Classes was used after Generics.Collections.

Solutions are:

  1. Change the order of your uses so that Generics.Collections appears after System.Classes. This is good practice no matter what. Or,
  2. Fully specify the type: Generics.Collections.TCollectionNotification.
like image 128
David Heffernan Avatar answered Sep 17 '22 15:09

David Heffernan