HomeDelphiDelphi Error – E2140 Duplicate message method index

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

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...