关于Tstrings的简单问题! (100分)

J

jbas

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码为什么会错?
//从文件读记录,以string数组的类型返回。
function TFrmSrvCfg.ReadCfgFile(FFile: string):tstrings;
var
CmtNeFile:TEXTFILE;
FileListStr:string;
Stigs:TStrings;
begin
try
Stigs:=TStrings.Create;
Assignfile(CmtNeFile,FFile);
if not fileexists(FFile) then
rewrite(CmtNeFile);
reset(CmtNeFile);
try
while not eof(CmtNeFile) do
begin
readln(CmtNeFile,FileListStr);
Stigs.Add(FileListStr);
end;
result:=Stigs;
finally
closefile(CmtNeFile);
end;
finally
Stigs.Free;
end;
end;

//调用这个函数.
Cflist:=Tstrings.create;
CfList:=ReadCfgFile(Glb_File);
CmbBxCmpNe.Items:=CfList;
LstBxCmt.Items:=CfList;
CfList.Free;
CmbBxCmpNe.ItemIndex:=0;
end;
 
finally
Stigs.Free;
end;

都释放掉了,还调用,不出错才怪呢...
 
是这个原因吗?注释掉了还不行。abstract error!错误
不是已经result:=Stigs;??
 
用TStringList就可以直接读文件呀
loadfromfile
 
我说我上面的错在那里呀,急呀,帮忙了!
 
Tstrings是一个抽象类,你怎么可能直接使用呢?
你应该用Tstringlist类。
其实错误提示很明确阿,这是一个抽象类,是不能被实例化的。
 
Tstrings是一个抽象类,你怎么可能直接使用呢?
你应该用Tstringlist类。
其实错误提示很明确阿,这是一个抽象类,是不能被实例化的。
 
我全部改成tstringlist也不行呀,提示地址错误!
 
用TStringList肯定可以了
注意返回函数值类型也要改
 
//调用这个函数.
// Cflist:=Tstrings.create
// 这句不要的,函数里已经有了
CfList:=ReadCfgFile(Glb_File);
// CmbBxCmpNe.Items:=CfList
// 应该用 CmbBxCmpNe.Items.Assign(CfList);
// LstBxCmt.Items:=CfList
// 同上理由
CfList.Free;
CmbBxCmpNe.ItemIndex:=0;
end;
 
小猪 is right!
 
谢谢各位,特别是小猪的大力帮忙。
已经解决。
函数function TFrmSrvCfg.ReadCfgFile(FFile: string):tstrings;
应该初始化,即加上
result:=Tstringlist.create;即可。
 
顶部