delphitcollection

Cannot override TCollection.Notify: Declaration of Notify differs from previous declaration


Note: The title of other question is different which prevents it from identifying as the matching one.

System.Classes

TCollection = class(TPersistent)
protected
  procedure Notify(Item: TCollectionItem; Action: TCollectionNotification); virtual;
end;

MyUnit

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  Vcl.ExtCtrls, DB, System.Generics.Collections;

TTextDisplayLineInfos = class(TCollection)
protected
  procedure Notify(Item: TCollectionItem; Action: TCollectionNotification); override; //Here "[dcc32 Error] MyUnit.pas(85): E2037 Declaration of 'Notify' differs from previous declaration"
end;

implementation

procedure TTextDisplayLineInfos.Notify(Item: TCollectionItem;
  Action: TCollectionNotification);
begin
  inherited; //Here "[dcc32 Error] MyUnit.pas(475): E2008 Incompatible types"
  //..............
end;

The signature of Notify method has been taken by copy-paste, so there couldn't be any errors;

Error

In interface section:

[dcc32 Error] MyUnit.pas(85): E2037 Declaration of 'Notify' differs from previous declaration

In implementation section:

[dcc32 Error] MyUnit.pas(475): E2008 Incompatible types

Question

Whai is wrong?


Solution

  • Unfortunately Delphi declares TCollectionNotification twice: one resides in System.Classes and another one in System.Generics.Collections.

    To workaround this issue, move System.Generics.Collections before System.Classes in your uses clause, or qualify it as System.Classes.TCollectionNotification).