出错提示是: [Warning] Unit5.pas(43): Variable 'Items' might not have been initialized [Pascal Error] Unit5.pas(1): Unable to invoke Code Completion due to errors in source code
[Warning] Unit5.pas(43): Variable 'Items' might not have been initialized 可能没有Items := TStringList.Create;语句就使用Items,或这建立的语句放在try、if等语句里了,因而引起的警告。[Pascal Error] Unit5.pas(1): Unable to invoke Code Completion due to errors in source code双击这里,看系统定位到哪条语句?
procedure TForm5.Button1Click(Sender: TObject); var i : integer; t : Cardinal; Items : TStrings; begin try t := GetTickCount(); if Trim(Edit1.Text)='' then begin Memo1.Text :='无效的查询'; Exit; end; Memo1.Lines.Clear(); Memo1.Text :='正在查询请稍候...'; Application.ProcessMessages(); Items := TStringList.Create; Items.LoadFromFile(form8.Edit1.Text); for i := Items.Count - 1 downto 0 do begin if Pos(edit1.text,Items.Strings[i])<=0 then Items.Delete(i); Application.ProcessMessages(); end; memo1.Lines.Assign(Items); memo1.Lines.Add(''); memo1.Lines.Add('查到:'+inttostr(Items.Count)+'处, ' + '耗时:'+floattostr((GetTickCount -t) / 1000.000)+'秒'); Items.Free; except if Assigned(Items) then Items.Free; memo1.Lines.Add('查找出错'); end; //try;end;
是这样的,为什么出错呢? [Warning] Unit5.pas(43): Variable 'Items' might not have been initialized
改为: procedure TForm5.Button1Click(Sender: TObject); var i : integer; t : Cardinal; Items : TStrings; begin t := GetTickCount(); if Trim(Edit1.Text)='' then begin Memo1.Text :='无效的查询'; Exit; end; Memo1.Lines.Clear(); Memo1.Text :='正在查询请稍候...'; Application.ProcessMessages(); Items := TStringList.Create; try Items.LoadFromFile(form8.Edit1.Text); for i := Items.Count - 1 downto 0 do begin if Pos(edit1.text,Items.Strings[i])<=0 then Items.Delete(i); Application.ProcessMessages(); end; memo1.Lines.Assign(Items); memo1.Lines.Add(''); memo1.Lines.Add('查到:'+inttostr(Items.Count)+'处, ' + '耗时:'+floattostr((GetTickCount -t) / 1000.000)+'秒'); Items.Free; except if Assigned(Items) then Items.Free; memo1.Lines.Add('查找出错'); end; //try; end;
这不是出错,仅是警告,“[Pascal Error]”这样的提示才是出错。
试想想,当你原来的代码: try t := GetTickCount();//假设运行到这一个句子出了错,因为是在try内,它必然要跳到except 后面执行,在except后面“if Assigned(Items) then ”这里访问到Items对象,所以,系统认为它还没建立就使用了,给你提出警告!
不过TStrings是抽象类,你要强转一下,TStringList(Items).
还有就是你的coadsight的时间有可能设置的比较长。
beginend;剪切、粘贴到QQ聊天室的发言栏(不用发送),再剪切、粘贴回delphi的代码编辑器,便可以发现在begin与end之间输入的对象,不能点出任何东西来了。
[Pascal Error] Unit5.pas(1): Unable to invoke Code Completion due to errors in source code
可能没有Items := TStringList.Create;语句就使用Items,或这建立的语句放在try、if等语句里了,因而引起的警告。[Pascal Error] Unit5.pas(1): Unable to invoke Code Completion due to errors in source code双击这里,看系统定位到哪条语句?
var
i : integer;
t : Cardinal;
Items : TStrings;
begin
try
t := GetTickCount();
if Trim(Edit1.Text)='' then
begin
Memo1.Text :='无效的查询';
Exit;
end;
Memo1.Lines.Clear();
Memo1.Text :='正在查询请稍候...';
Application.ProcessMessages();
Items := TStringList.Create;
Items.LoadFromFile(form8.Edit1.Text);
for i := Items.Count - 1 downto 0 do
begin
if Pos(edit1.text,Items.Strings[i])<=0 then
Items.Delete(i);
Application.ProcessMessages();
end;
memo1.Lines.Assign(Items);
memo1.Lines.Add('');
memo1.Lines.Add('查到:'+inttostr(Items.Count)+'处, '
+ '耗时:'+floattostr((GetTickCount -t) / 1000.000)+'秒');
Items.Free;
except
if Assigned(Items) then Items.Free;
memo1.Lines.Add('查找出错');
end; //try;end;
[Warning] Unit5.pas(43): Variable 'Items' might not have been initialized
procedure TForm5.Button1Click(Sender: TObject);
var
i : integer;
t : Cardinal;
Items : TStrings;
begin
t := GetTickCount();
if Trim(Edit1.Text)='' then
begin
Memo1.Text :='无效的查询';
Exit;
end;
Memo1.Lines.Clear();
Memo1.Text :='正在查询请稍候...';
Application.ProcessMessages();
Items := TStringList.Create;
try
Items.LoadFromFile(form8.Edit1.Text);
for i := Items.Count - 1 downto 0 do
begin
if Pos(edit1.text,Items.Strings[i])<=0 then
Items.Delete(i);
Application.ProcessMessages();
end;
memo1.Lines.Assign(Items);
memo1.Lines.Add('');
memo1.Lines.Add('查到:'+inttostr(Items.Count)+'处, '
+ '耗时:'+floattostr((GetTickCount -t) / 1000.000)+'秒');
Items.Free;
except
if Assigned(Items) then Items.Free;
memo1.Lines.Add('查找出错');
end; //try;
end;
try
t := GetTickCount();//假设运行到这一个句子出了错,因为是在try内,它必然要跳到except
后面执行,在except后面“if Assigned(Items) then ”这里访问到Items对象,所以,系统认为它还没建立就使用了,给你提出警告!
+1应该把
Items : TStrings;
定义成
Items : TStringList;
这样吧。你再试试
后来又好了估计是ide的问题,但是无从下手就像:在ide里运行几次后,再编译运行就报错。。必须关闭d7再启动d7才行
无提示的问题出现的比较少,这个问题困扰了很多不过,刚才为了试验,却运行了n次也没出现。。上午还几乎每运行2次必须关闭
Exception:
------------------------------------------------------------------------------------------------------------
2.1 Date : Wed, 28 Sep 2011 17:32:29 +0800
2.2 Address : 0083ACF0
2.3 Module Name : vcl70.bpl - (Borland Component Package)
2.4 Module Version: 7.0.8.1
2.5 Type : EAccessViolation
2.6 Message : Access violation at address 0083ACF0 in module 'vcl70.bpl'. Write of address 00000018.
2.7 ID : 4D39
2.8 Count : 1
2.9 Status : New
2.10 Note :