Posted by webmaster Guido on December 06, 2007
In Reply to function overloading turbo delphi explorer posted by Newb p15701 on
December 05, 2007
: I've been trying to overload functions in my Delphi prog, but I always get errors
:(
: Can somebody post some working source code?
: Maybe it is because I use Turbo Delphi "Explorer", the free version, does it support overloading?
Turbo Delphi Explorer allows overloading, like all payed as well as free versions since Delphi 4. So, something must be wrong with your source code ;)
Here's a source code example:
function ValToString(X: string): string; overload;
begin
Result := X;
end;
function ValToString(X: integer): string; overload;
begin
Result := IntToStr(X);
end;
function ValToString(X: real): string; overload;
begin
Result := FloatToStr(X);
end;
function ValToString(X: real; Decimals: integer): string; overload;
begin
Result := FloatToStrF(X, ffFixed, 10, Decimals);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := ValToString('ABC');
Label2.Caption := ValToString(2);
Label3.Caption := ValToString(2.5);
Label4.Caption := ValToString(2.5, 2);
end;
The rules:
1. In the implementation section, the "header" of an overloaded routine must be followed by: overload;
...EXCEPT if it's about "methods": then only in the interface section you must add overload after the definitions of the overloaded methods, such as here:
type
TForm1 = class(TForm)
...
private
procedure ShowValue(S: string); overload;
procedure ShowValue(R: real); overload;
...
2. Overloaded routines must differ in their parameters. The number of parameters must be different, or their types (string, integer, real, date,...) must be different.
Important note: the names of the parameters play no role. Two routines with different names for their parameters but with the same types of parameters, result in an error. Likewise, passing an argument "by value" and another "by reference" is unimportant.
For more info, look also at this thread, starting with message 13105.
Related articles
Follow Ups
Post a Followup
DelphiLand Club members: enter your Membership password.
Guests can get a Forum Guest password by subscribing to the DelphiLand Newsletter.