送分题, inifile的使用,请帮助!!(50分)

  • 主题发起人 张剑波
  • 开始时间

张剑波

Unregistered / Unconfirmed
GUEST, unregistred user!
如下:
uses inifiles
......
function xxxxxxx:xxx
begin
var
myini:tinifile;
begin
try
myini:=tinifile.create( 'aaa.ini');
do something
finally
myini.free;
end;
end;
这一段编译后提示: 'myini' might not have been initialized
我在程序中大量使用类似语句,这样的提示有10几个。
当然运行没发现什么问题,不过心里不舒服。
我有点神经质了,你说说吧,这段程序有毛病吗?


 
function xxxxxxx:xxx
begin
var
myini:tinifile;
begin
myini:=nil; //那就给它初始化呗:)
try

。。。。。
 
在全局变量中定义myini试试
 
给你个例子:
功能:将Combobox项目保存入Ini文件,保存前先判断要保存内容是否为空,如为空则不保存
还要判断要保存项目在列表中是否存在,如存在,则不保存}
procedure TForm1.SetIniFile;
var
IniFile : TIniFile;
RenYuanList : string;
begin
{如为空,则不保存}
if Trim(ComboBox1.Text) = '' then
Exit;
IniFile := TIniFile.Create('E:/IniFile.Ini');
try
{将Ini文件列表中内容读出}
RenYuanList := IniFile.ReadString('人员列表', Trim(Label1.Caption), '');
{判断要保存项目是否已存在,如果存在,则不保存}
if Pos(Trim(ComboBox1.Text), RenYuanList) > 0 then
Exit;
{生成要保存的人员名字列表,用'#'分隔}
if RenYuanList = '' then
RenYuanList := Trim(ComboBox1.Text)
else
RenYuanList := RenYuanList + '#' + Trim(ComboBox1.Text);
{将人员列表写入INI文件}
IniFile.WriteString('人员列表', Trim(Label1.Caption), RenYuanList);
finally
IniFile.Free;
end;
end;


{将INI文件中的内容读出到Combobox中}
procedure TForm1.GetIniFile;
var
IniFile : TIniFile;
RenYuanString : string;
begin
{引用要使用的INI文件}
IniFile := TIniFile.Create('E:/IniFile.Ini');
try
{从INI文件读出人员列表}
RenYuanString := IniFile.ReadString('人员列表', Label1.Caption, '');
{如果人员列表为空,则退出}
if RenYuanString = '' then
Exit;
{将人员列表转化为以回车分隔的字符串}
RenYuanString := StringReplace(RenYuanString, '#', #13#10, [rfReplaceAll]);
{将字符串导入Combobox中}
ComboBox1.Items.Text := RenYuanString;
finally
IniFile.Free;
end;
end;

 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
688
import
I
I
回复
0
查看
563
import
I
D
回复
0
查看
1K
DelphiTeacher的专栏
D
顶部