【问题标题】:How to save strings in a stringlist which is created in another procedure?如何将字符串保存在另一个过程中创建的字符串列表中?
【发布时间】:2013-03-13 14:40:22
【问题描述】:

我的程序需要一些帮助。我想将一些字符串保存在另一个过程中创建的字符串列表中。我该怎么做?

我在正确的地方写了一条评论以便更好地理解它。

procedure GetIniNamesWithoutExt(IniPfade: TStringList);
var
  i, suchPunkt: integer;
  ini: TIniFile;
  Modul, fullFileName, IniName: String;
begin
  try
  for i := 0 to IniPfade.Count-1 do
  begin
    fullFileName := IniPfade.Strings[i];
    Modul := ExtractFileName(fullFileName);  // Dateiname aktueller Ini + .Ini Endung
    suchPunkt := Pos('.', Modul);
    IniName := Copy(Modul, 1, suchPunkt-1);  // Aktueller Modulname ohne ini Endung
    // Here should be the Code for saving the String "IniName" to a StringList which is created in procedure a. Procedure a calls the procedure GetIniNamesWithoutExt.
  end;
  finally

  end;
end;

【问题讨论】:

  • 我不确定您的意图是什么,但如果您要将修改后的字符串保存到作为参数传递的IniPfade 字符串列表中,只需通过IniPfade[i] := IniName; 执行即可,但是我猜你的问题是关于其他的。
  • 我在另一个过程“a”中创建了一个字符串列表,并希望将我的 var IniName 保存到过程 a 中存在的 StringList...

标签: delphi procedure delphi-xe3 tstringlist


【解决方案1】:

怎么样

procedure GetIniNamesWithoutExt(IniPfade, Module: TStrings);
var
  i, suchPunkt: integer;
  ini: TIniFile;
  Modul, fullFileName, IniName: String;
begin
  Module.BeginUpdate;
  try
    for i := 0 to IniPfade.Count-1 do
    begin
      fullFileName := IniPfade.Strings[i];
      Modul := ExtractFileName(fullFileName);  // Dateiname aktueller Ini + .Ini Endung
      suchPunkt := Pos('.', Modul);
      IniName := Copy(Modul, 1, suchPunkt-1);  // Aktueller Modulname ohne ini Endung
      Module.Add(IniName);
    end;
  finally
    Module.EndUpdate;
  end;   
end;

从程序 A:

procedure A;
var 
  Module: TStringList;
begin
  Module := TStringList.Create;
  try
    GetIniNamesWithoutExt(IniPfade , Module);
    // Do Whatever you want with "Module"
  finally
    Module.Free;
  end;
end;

【讨论】:

  • 我为TStringList 添加了一个try..finally 块。
  • 另一种选择:声明我的 StringList 全局并在过程 a 中创建它。
  • @Smasher,谢谢你的补充——我打字有点仓促。哪里有 Create finally 应该是一个 Free !
  • @iamjoosy 我刚刚将TStringList 更改为TStrings,还为Module 添加了BeginUpdateEndUpdate。现在可以通过例如TComboBox.Items 作为模块参数
  • 不用PosCopy 来删除扩展名,你可以使用ChangeFileExt(fullFileName, ''); 代替。
猜你喜欢
  • 2021-08-09
  • 2017-02-13
  • 1970-01-01
  • 2012-01-11
  • 2013-08-11
  • 1970-01-01
  • 2019-08-26
  • 2018-06-06
  • 2017-02-21
相关资源
最近更新 更多