Re: is overload invalid for methods?

Posted by Jenny on June 03, 2007

In Reply to is overload invalid for methods? posted by Diane Channers on June 02, 2007

Why do I get this error:
"Method FirstPart with identical parameters already exists"?
This is what I have in the private section of the unit of Form1:
function FirstPart(S: string): string; overload;
procedure FirstPart(var S2: string); overload;

You get an error because the number of parameters is the same in both routines, and they are of the same type (in both routines of the type "string"). On the other hand, the different names of the parameters have nothing to do with it, nor the way how you "pass" them, nor the kind of routine (function or procedure).

We can easily demonstrate this by slightly altering your code. Just add a parameter to the procedure declaration, like this:

function FirstPart(S: string): string; overload; 
procedure FirstPart(var S2: string; Dummy: string); overload;
  
function TForm1.FirstPart(S: string): string;
begin
  Result := Copy(S, 1, Pos('/', S) - 1);
end;
  
procedure TForm1.FirstPart(var S2: string; Dummy: string);
begin
  S2 := Copy(S2, 1, Pos('/', S2) - 1);
end;

In the example above, parameter Dummy is not used, its only purpose is for showing the effect of having a different number of parameters.

Just as an aside note: I would recommend not to use the same name for a function and a procedure, as this can be quite confusing later on whe you review your code ;)

Greetings,
Jenny, DelphiLand Team

Related articles

       

Follow Ups