Missing Operator or Semicolon error
Asked Answered
B

1

5

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?

Batsman answered 23/3, 2014 at 18:53 Comment(0)
P
8

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().
Patrimony answered 23/3, 2014 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.