0

Consider this code :

  TForm3 = class(TForm)
  public
   class procedure GetAConn(var res:String); overload;
   class procedure GetAConn(var res:Integer);overload;
    { Public declarations }
  end;

class procedure TForm3.GetAConn(var res: String);
begin
 showmessage(res);
end;

class procedure TForm3.GetAConn(var res: Integer);
begin
 showmessage(IntToStr(res))
end;

which compiles without any problems.

Now, If I'm doing this :

procedure TForm3.FormCreate(Sender: TObject);
begin
 TForm3.GetAConn('aaa');
 TForm3.GetAConn(10);
end;

I get [DCC Error] Unit3.pas(64): E2250 There is no overloaded version of 'GetAConn' that can be called with these arguments.

I didn't find something about this being restricted in Delphi XE.

LE: is working in this way:

class procedure TForm3.GetAConn(var res: String);
begin
 res := res + 'modif';
end;

class procedure TForm3.GetAConn(var res: Integer);
begin
 res := res + 100;
end;
procedure TForm3.FormCreate(Sender: TObject);
var s:String;
    i:Integer;
begin
 s:='aaa';
 TForm3.GetAConn(s);
 showmessage(s);
 i:=10;
 TForm3.GetAConn(i);
 showmessage(IntToStr(i))
end;
RBA
  • 12,337
  • 16
  • 79
  • 126

1 Answers1

7

You're passing the parameters by reference. Drop the var and all should be well:

class procedure GetAConn(const res: String); overload;
class procedure GetAConn(res: Integer); overload;

(As you don't modify the string parameter I suggest passing it as const.)

If you indeed need reference parameters than you can't pass literals or constants, of course. But that has nothing to do with using overload. (Besides the fact that overloading obsfuscates the error message.)

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
  • +1 for the answer.If I need to pass the params by reference it would not work? – RBA Nov 09 '12 at 11:24
  • The Integer isn't modified either. May as well pass it as `const` too. – David Heffernan Nov 09 '12 at 11:26
  • @David: That's true but passing const Integers isn't Delphi idiomatic. Maybe it's time to change that idiom. :-) – Uli Gerhardt Nov 09 '12 at 11:56
  • 3
    The main reason you prefer it for `strings` is to avoid the overhead associated with by value passing of managed types. It's a shame that particular performance consideration gets conflated with parameter semantics. – David Heffernan Nov 09 '12 at 12:00
  • it's logic. It seems that I need to get some sleep. Thanks all. – RBA Nov 09 '12 at 12:15