9

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?

Arnold
  • 4,578
  • 6
  • 52
  • 91

1 Answers1

14

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.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • You omitted one source of error: having declared System.Classes and not System.Generics.Collections which happened in this case :-) You helped me to find the culprit, thanks! There are some nasty errors associated with this mix up. I'll follow your advice and always declare System.Classes first. – Arnold Nov 08 '13 at 20:12
  • That's not a source of error. If you omit `Generics.Collections` then you get a different failure. Then your failure is that `TList` is not defined. – David Heffernan Nov 08 '13 at 20:13
  • In this situation it is. There was a `system.classes` declaration in the mainform but no `system.generics.collection` that caused the compiler error. The `tlist` is declared in another unit. Adding a system.generics.collection solved the error. – Arnold Nov 08 '13 at 22:01
  • Well, I couldn't see those details. By the way, making `FEditors` public seems wrong. Is the code really like that? – David Heffernan Nov 08 '13 at 22:02