Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the compiler say "Too many actual parameters" when I think I've provided the correct number?

I've declared the following function:

function next(current, next: string): Integer;
begin
    form1.Label1.Caption := next;
    form1.Label2.Caption := current;
    form1.label3.Caption := clipboard.AsText+inttostr(c);
    Result:=1;
end;

I try to execute it with this code:

if label1.Caption = '' then res := next('current', 'next');

I am getting the following error:

[Error] Unit1.pas(47): E2034 Too many actual parameters

I think all parameters are good, so why am I getting that error?

like image 927
djcis Avatar asked Dec 10 '22 12:12

djcis


2 Answers

I just tried your code on both Delphi 7 and Delphi 2010. If it works on those two, it should also work on Delphi 2005.

Conclusion: Delphi wants to use a different version of the "next" routine, because of code scope/visibility. Try ctrl+click-ing on "next" in "res := next();" and see where Delphi takes you. Alternatively post more code so we can tell you why Delphi is not choosing your version of the "next" routine. Ideally you should post a whole unit, starting from "unit name" to the final "end."

like image 103
Cosmin Prund Avatar answered Dec 15 '22 00:12

Cosmin Prund


As specified by Cosmin Prund, the problem is because of the visibility.

TForm has a procedure with name Next which wont accept any parameters.

Your function uses the same name and as you are calling the function in TForm1 class implementation, compiler is treating the call as TForm1.Next and hence it was giving error.

To solve the problem, precede the unit name before the function name i.e., Unit1.Next().

So this should be your code

if label1.Caption = '' then res := Unit1.next('current', 'next');
like image 38
Bharat Avatar answered Dec 14 '22 22:12

Bharat