Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing Operator or Semicolon error

Delphi 7 if it matters.

procedure writeLog ( varName, varValue: string );
var
  length, i :Integer;
begin
  Append( logFile );
  ShowMessage (varName);
  For i := Length(varName) to 20 do       //line 37
  begin
     varName := varName + ' ';
  end;
  WriteLn( logFile, varName + ': ' + varValue );
  CloseFile( logFile );
end;

I get the error:

[Error] felirat.dpr(37): Missing operator or semicolon

All the semicolons seem just fine to me. What am I missing?

like image 521
András Avatar asked Mar 21 '23 03:03

András


1 Answers

You declared a local variable named length. This local variable hides the function of the same name declared in the System unit (Delphi, as a Pascal derivative, is case-insensitive). So when you wrote:

For i := Length(varName) to 20 do      

the compiler sees Length as the variable rather than the function. And that leads to your compiler error.

Possible solutions:

  • Use a different name for the variable, e.g. len.
  • Use the fully scoped name for the function: System.Length().
like image 69
MBo Avatar answered Mar 27 '23 15:03

MBo