2

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?

Mike Torrettinni
  • 1,816
  • 2
  • 17
  • 47
Paul
  • 25,812
  • 38
  • 124
  • 247
  • 1
    Right-click on the erroneous declaration. The top item in the context menu that shows up should be something like "Find Declaration". Click that and find the "previous" declaration and see if it differs. The reason could be that your declaration in the interface and implementation section don't match, or that there is another class TCollection closer in scope than the TCollection you want to extend, or a different declaration of TCollectionNotification, or... or... – Rudy Velthuis Jan 19 '18 at 16:17
  • Then there must be a TCollection inbetween. Find the declaration of TCollection and see if it is the one you expect it to be. Also, clear your project and see if there are different files with the same name (i.e. a different version of your unit code somewhere, being picked up) in a directory in the search path. I use the free [Everything](http://www.voidtools.com) for such a search. It is extremely fast (usually immediate result). Also check if there is a different TCollectionNotification somewhere in scope. – Rudy Velthuis Jan 19 '18 at 16:46
  • Possible duplicate of [How to inherit generic virtual method?](https://stackoverflow.com/questions/37308109/how-to-inherit-generic-virtual-method) – J... Jan 19 '18 at 17:22

1 Answers1

10

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).

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130