Unobvious Delphi syntax error

点点圈 提交于 2021-01-29 11:50:39

问题


I have a problem with some simple code I simply cannot resolve. I'm using Delphi 5 with all updates applied.

I have this code block:

procedure TForm1.LoadBtnClick(Sender: TObject);
var
   s1, s2, s3 : Textfile;
   sa, sb, RString, SQLString : string;
begin
  with sourcequery do begin
     Close;
     SQL.Clear;
     SQL.Add('delete * from source');
     ExecSQL;
     Close;
     AssignFile(S2, 'c:\delphi programs\events\source1.txt');
     AssignFile(S1, 'c:\delphi programs\events\source2.txt');
     Reset(s2);
     Reset(s1);
     while not eof(s1) do begin
        readln(s1, RString);
        SQLString := 'insert into source1(f1, f2) values(';
        SQLstring := SQLString+ QuotedStr(copy(Rstring, 1, pos(chr(9), Rstring)-1))+', ';
        SQLString := SQLString+QuotedStr(copy(Rstring, pos(chr(9),Rstring)+1,length(Rstring)))+')';
        with sourcequery do begin
           close;
           SQL.Clear;
           SQL.Add(SQLString);
           ExecSQL;
        end;
     end;
  end;
end;

When I try to compile it, though, it halts after "eof(" on this line:

 while not eof(s1) do begin

with the "missing operator or semi-colon" error message.

Try as I might I can find no error - missing operators OR semi-colons. I have commented the file-reading block out and it compiles fine. I've retyped it a number of times in case there were hidden special characters. So far this is the entire program apart from the header information.

Can anyone spot the silly error?


回答1:


All your code is wrapped in a with sourcequery do begin block, so any identifier within that block is first checked for whether it could be a member of sourcequery. That variable, evidently, has a member named eof that isn't a function. The compiler binds the eof name to that non-function instead of the function you expected it to be bound to, and therefore complains when you attempt to use it like a function.

The best solution is to stop using with. It leads to problems like this, where the apparent scope of names doesn't match the one the compiler sees. It also leads to problems debugging (because of a long-standing bug in the debugger, where it interprets names the way you did here, rather than the way the compiler interprets them). It can lead to other bugs, too. If you don't like typing sourcequery.xyz all the time, then you can create an alias for sourcequery that doesn't take up so much room:

var
  sq: TypeOfSourceQuery;
begin
  sq := sourcequery;
  sq.Close;
  sq.SQL.Clear;
  ...

If you really don't want to use that solution, then you can use the quick-and-easy solution, which is to qualify the eof name with the unit it belongs to. In your case, you want the function defined in the System unit, so change your code to indicate that:

while not System.Eof(s1) do begin


来源:https://stackoverflow.com/questions/23294552/unobvious-delphi-syntax-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!