1

Sometimes I get "E2251 Ambiguous overloaded call to 'MyMethodName'" when passing open arrays to overloaded methods.

Example:

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure Test(AArrA : array of integer); overload;
    procedure Test(AArrA : array of TObject); overload;
  end;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  Test([10, 15]);
end;

It produces the following error:

[DCC Error] Unit1.pas(37): E2251 Ambiguous overloaded call to 'Test'

Until now, I've always used a temporary dynamic array due to avoid the error but I don't find it very clean

procedure TForm1.FormCreate(Sender: TObject);
var
  Tmp : array of integer;
begin
  SetLength(Tmp, 2);
  Tmp[0] := 10;
  Tmp[1] := 15;

  Test(Tmp);
end;

Is there a cleaner way to help Delphi discerning between overloads which accepts open array parameters?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • 1
    Can't reproduce. The code shown compiles and executes properly in modern versions of the compiler. It must be a bug in D2007 that was already fixed in a later version. – Remy Lebeau Mar 19 '18 at 17:29
  • @RemyLebeau: Happy to know that it is no more a problem in newer versions. Anyhow, is there any workaround for Delphi2007? – Fabrizio Mar 19 '18 at 18:25
  • 2
    The only workaround is the one you already found - use a local variable for the array. You don't have to use a dynamic array, though. You can use a static array instead: `var Tmp : array[0..1] of integer; Tmp[0] := 10; Tmp[1] := 15; Test(Tmp);` – Remy Lebeau Mar 19 '18 at 18:31

0 Answers0