TStringList的addObject方法

Kum*_*r S 11 delphi

我想知道这个方法调用的作用:

stringList.addObject(String,Object);
Run Code Online (Sandbox Code Playgroud)

我也想知道这个属性的作用:

stringList.Objects[i]
Run Code Online (Sandbox Code Playgroud)

添加时看起来像键,值对.但是在循环中检索什么被检索?

我也看到[i]电话.

我对TStringList操作和TList操作感到困惑.

Ken*_*ite 17

它增加了对项目的:在一个条目TStringList.Strings列表,并匹配TObjectTStringList.Objects列表中.

例如,这允许您存储为项提供名称的字符串列表,以及包含匹配项的类的对象.

type
  TPerson=class
    FFirstName, FLastName: string;
    FDOB: TDateTime;
    FID: Integer;
  private
    function GetDOBAsString: string;
    function GetFullName: string;
  published
    property FirstName: string read FFirstName write FFirstName;
    property LastName: string read FLastName write FLastName;
    property DOB: TDateTime read FDOB write FDOB;
    property DOBString: string read GetDOBAsString;
    property FullName: string read GetFullName;
    property ID: Integer read FID write FID;
  end;

implementation

{TPerson}
function TPerson.GetDOBAsString: string;
begin
  Result := 'Unknown';
  if FDOB <> 0 then
    Result := DateToStr(FDOB);
end;

function TPerson.GetFullName: string;
begin
  Result := FFirstName + ' ' + FLastName; // Or FLastName + ', ' + FFirstName
end;

var
  PersonList: TStringList;
  Person: TPerson;
  i: Integer;
begin
  PersonList := TStringList.Create;
  try
    for i := 0 to 9 do
    begin
      Person := TPerson.Create;
      Person.FirstName := 'John';
      Person.LastName := Format('Smith-%d', [i]); // Obviously, 'Smith-1' isn't a common last name.
      Person.DOB := Date() - RandRange(1500, 3000);  // Make up a date of birth
      Person.ID := i;
      PersonList.AddObject(Person.LastName, Person);
    end;

    // Find 'Smith-06'
    i := PersonList.IndexOf('Smith-06');
    if i > -1 then
    begin
      Person := TPerson(PersonList[i]);
      ShowMessage(Format('Full Name: %s, ID: %d, DOB: %s',
                         [Person.FullName, Person.ID, Person.DOBString]));
    end;
  finally
    for i := 0 to PersonList.Count - 1 do
      PersonList.Objects[i].Free;
    PersonList.Free;
  end;
Run Code Online (Sandbox Code Playgroud)

这显然是一个人为的例子,因为它不是你真正觉得有用的东西.然而,它证明了这一概念.

另一个方便的用途是用于存储整数值和字符串(例如,显示一个TComboBox或多个项中的项列表TListBox以及用于数据库查询的相应ID).在这种情况下,您只需要对Objects数组中的整数(或其他任何SizeOf(指针))进行类型转换.

// Assuming LBox is a TListBox on a form:
while not QryItems.Eof do
begin
  LBox.Items.AddObject(QryItem.Fields[0].AsString, TObject(QryItem.Fields[1[.AsInteger));
  QryItems.Next;
end;

// User makes selection from LBox
i := LBox.ItemIndex;
if i > -1 then
begin
  ID := Integer(LBox.Items.Objects[i]);
  QryDetails.ParamByName('ItemID').AsInteger := ID;
  // Open query and get info.
end;
Run Code Online (Sandbox Code Playgroud)

在存储非实际TObject内容的情况下,您不需要释放内容.因为它们不是真正的物体,除了它TStringList本身之外没有什么可以解放的.

  • 较新的Delphi版本具有TStringList,其具有与OwbjectList类似的OwnsObjects属性(和构造函数参数).如果将其设置为true,则实际上您不应该在释放列表之前释放实例. (2认同)

RRU*_*RUZ 5

AddObject方法允许您存储与Item属性中存储的字符串关联的TObject地址(指针).该Objects属性用于访问存储的对象.

检查这个简单的samplem,它使用它AddObject来存储与每个字符串关联的整数值.

var
 List : TStringList;
 I    : integer;
begin
  try
    List:=TStringList.Create;
    try
      List.AddObject('Item 1', TObject(332));
      List.AddObject('Item 2', TObject(345));
      List.AddObject('Item 3', TObject(644));
      List.AddObject('Item 4', TObject(894));

      for I := 0 to List.Count-1 do
        Writeln(Format('The item %d contains the string "%s" and the integer value %d',[I, List[I], Integer(List.Objects[i])]));
    finally
      List.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
Run Code Online (Sandbox Code Playgroud)