Delphi Compiler Error
E2140 Duplicate message method index
Reason for the Error & Solution
You have specified an index for a dynamic method which is already used by another dynamic method.
program Produce; type Base = class procedure First(VAR x : Integer); message 151; procedure Second(VAR x : Integer); message 151; end; procedure Base.First(VAR x : Integer); begin end; procedure Base.Second(VAR x : Integer); begin end; begin end.
The declaration of ‘Second’ attempts to reuse the same message index which is used by ‘First’; this is illegal.
program Solve; type Base = class procedure First(VAR x : Integer); message 151; procedure Second(VAR x : Integer); message 152; (*change to unique index*) end; Derived = class (Base) procedure First(VAR x : Integer); override; (*override base class behavior*) end; procedure Base.First(VAR x : Integer); begin end; procedure Base.Second(VAR x : Integer); begin end; procedure Derived.First(VAR x : Integer); begin end; begin end.
There are two straightforward solutions to this problem. First, if you really do not need to use the same message value, you can change the message number to be unique. Alternatively, you could derive a new class from the base and override the behavior of the message handler declared in the base class. Both options are shown in the above example.