不明显的Delphi语法错误 [英] Unobvious Delphi syntax error

查看:50
本文介绍了不明显的Delphi语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一些我无法解决的简单代码有疑问.我正在使用已应用所有更新的Delphi 5.

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

我有这个代码块:

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;

但是,当我尝试编译它时,它在此行的"eof("之后停顿了:

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?

推荐答案

所有代码都包装在中,并且带有sourcequery do begin 块,因此首先检查该块中的所有标识符是否可以成为 sourcequery 的成员.显然,该变量具有一个名为 eof 的成员,该成员不是函数.编译器将 eof 名称绑定到该非函数而不是您希望将其绑定到的函数,因此,当您尝试将其像函数一样使用时会抱怨.

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.

最好的解决方案是停止使用 with .这会导致这样的问题,其中名称的表观范围与编译器看到的范围不匹配.它还会导致调试问题(由于调试器中存在一个长期存在的错误,调试器在此处以您在此处所做的方式解释名称,而不是编译器对其进行解释的方式).它也可能导致其他错误.如果您不喜欢一直输入 sourcequery.xyz ,则可以为 sourcequery 创建一个不占用太多空间的别名:

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;
  ...

如果您真的不想使用该解决方案,则可以使用快速简便的解决方案,即使用其所属的单位来限定 eof 名称.在您的情况下,您需要在 System 单元中定义的功能,因此请更改代码以指示以下内容:

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

这篇关于不明显的Delphi语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆