Using TStringList's AddObject with integers?

后端 未结 1 840
粉色の甜心
粉色の甜心 2021-02-13 20:15

Using delphi 7:

  • How can I add an integer to the object portion of a stringlist item, using AddObject?
  • How can I retrieve the integer back fro
相关标签:
1条回答
  • 2021-02-13 20:26

    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 讨论(0)
提交回复
热议问题