12

使用德尔福 7:

  • 如何将整数添加到字符串列表项的对象部分,使用AddObject
  • 如何从 stringlist 项的对象属性中检索整数?
  • 完成后如何释放所有对象和列表?
4

1 回答 1

42

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

A:只需将整数值转换为TObject

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

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

A:将对象值转换为整数

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

问: 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.
于 2011-08-30T01:16:48.063 回答