Delphi Error – E2140 Duplicate message method index

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.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...