Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

text files - Unobvious Delphi syntax error

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 programseventssource1.txt');
     AssignFile(S1, 'c:delphi programseventssource2.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...