开发者

Using TStringList's AddObject with integers?

Using delphi 7:

  • How can I add an integer to the object portion of a stringlist item, using AddObject?
  • How can I retriev开发者_如何学运维e the integer back from a object property of stringlist item?
  • How do I free all objects and list when done?


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

A: Just cast the integer value to TObject

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

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

A: Cast to integer the Object Value

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

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

A: You don't need free the object list, because you are not assigning memory. so only call the Free procedure of the TStringList.

Try this sample app

{$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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜