将 TStringList 的 AddObject 与整数一起使用?

2024-04-12

使用德尔福7:

  • 如何将整数添加到字符串列表项的目标部分, 使用AddObject?
  • 如何从对象中检索整数 字符串列表项的属性?
  • 如何释放所有对象并列出 什么时候完成?

Q: How can i add an integer to the object portion of a stringlist item, using AddObject?

A:只需将整数值转换为TObject http://docwiki.embarcadero.com/VCL/en/System.TObject

List.AddObject('A string',TObject(1));

Q:How can a retrieve the integer back from a object property of stringlist item?

A:将对象值转换为整数

AValue := Integer(List.Objects[i]);

Q: How do i free all objects and list when done?

A:您不需要释放对象列表,因为你没有分配内存。所以只需要调用Free程序的TStringList.

尝试这个示例应用程序

{$APPTYPE CONSOLE}

uses
  Classes,
  SysUtils;


Var
  List : TStringList;
  i    : Integer;
begin
  try
    List:=TStringList.Create;
    try
      //assign the string and some integer values
      List.AddObject('A string',TObject(1));
      List.AddObject('Another string',TObject(100));
      List.AddObject('And another string',TObject(300));

      //Get the integer values back   

       for i:=0 to List.Count - 1 do
         Writeln(Integer(List.Objects[i]));

    finally
      //Free the list  
      List.free;
    end;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;

  Readln;
end.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 TStringList 的 AddObject 与整数一起使用? 的相关文章

随机推荐