Delphi TList of records

后端 未结 8 1012
遥遥无期
遥遥无期 2020-12-13 02:55

I need to store a temporary list of records and was thinking that a TList would be a good way to do this? However I am unsure how to do this with a TList<

8条回答
  •  佛祖请我去吃肉
    2020-12-13 03:16

    It all depends on the type of data you want to store.

    You might consider using TCollection and TCollectionItem.

    Here is (edited) code from a working unit, in which I used TCollection to read a list of report definitions from a folder. Each report consisted of a sort of template and an SQL statement which had to be stored together with a file name.

    Since it is edited, and uses some of my own units (TedlFolderRtns reads files into an internal list, to name but one), the example is simple enough to be useful. With a few replace all, you can adapt to whatever your need.

    Look up TCollection in the help, you can do a lot with it. And it keeps your code handling nicely grouped together in a class-like structure.

      unit cReports;
      interface
      uses
         SysUtils, Classes, XMLDoc, XMLIntf, Variants,
         // dlib - Edelcom
         eIntList, eProgSettings,eFolder ;
      type
    
         TReportDefItem = class(TCollectionItem)
         private
            fSql: string;
            fSkeleton: string;
            fFileName: string;
            procedure Load;
            procedure SetFileName(const Value: string);
         public
            constructor Create(Collection:TCollection); override;
            destructor Destroy ; override;
    
            property FileName: string read fFileName write SetFileName;
            property Sql : string read fSql write fSql;
            property Skeleton : string read fSkeleton write fSkeleton;
         end;
    
         TReportDefList = class(TCollection)
         private
            function OsReportFolder: string;
            function GetAction(const Index: integer): TReportDefItem;
         public
            constructor Create(ItemClass: TCollectionItemClass);
            destructor Destroy; override;
    
            procedure LoadList;
    
            function Add : TReportDefItem;
            property Action [ const Index:integer ]: TReportDefItem read GetAction;
         end;
    
      implementation
    
      { TReportDefList }
    
      constructor TReportDefList.Create(ItemClass: TCollectionItemClass);
      begin
         inherited;
      end;
    
      destructor TReportDefList.Destroy;
      begin
         inherited;
      end;
      function TReportDefList.Add: TReportDefItem;
      begin
         Result := TReportDefItem( Add() );
      end;
    
      function TReportDefList.GetAction(const Index: integer): TReportDefItem;
      begin
         if (Index >= 0) and (Index < Count)
         then Result := TReportDefItem( Items[Index] )
         else Result := Nil;
      end;
    
      procedure TReportDefList.LoadList;
      var Folder : TedlFolderRtns;
          i : integer;
          Itm : TReportDefItem;
      begin
         Folder := TedlFolderRtns.Create;
         try
            Folder.FileList( OsReportFolder,'*.sw.xml', False);
            for i := 0 to Folder.ResultListCount -1 do
            begin
              Itm := Add();
              Itm.FileName := Folder.ResultList[i];
            end;
         finally
            FreeAndNil(Folder);
         end;
      end;
    
      function TReportDefList.OsReportFolder: string;
      begin
         Result := Application.ExeName + '_RprtDef';
      end;
    
      { TReportDefItem }
    
      constructor TReportDefItem.Create(Collection: TCollection);
      begin
         inherited;
         fSql := '';
         fSkeleton := '';
      end;
    
      destructor TReportDefItem.Destroy;
      begin
        inherited;
      end;
    
      procedure TReportDefItem.Load;
      var XMLDoc : IXMLDocument;
          TopNode : IXMLNode;
          FileNode : IXmlNode;
          iWebIndex, iRemoteIndex : integer;
          sWebVersion, sRemoteVersion: string;
          sWebFileName: string;
      begin
         if not FileExists(fFileName ) then Exit;
    
         XMLDoc := TXMLDocument.Create(nil);
         try
            XMLDoc.LoadFromFile( fFileName );
            XMLDoc.Active := True;
    
            TopNode := XMLDoc.ChildNodes.FindNode('sw-report-def');
            if not Assigned(TopNode) then Exit;
    
            FileNode := TopNode.ChildNodes.First;
            while Assigned(FileNode) do
            begin
               fSql := VarToStr( FileNode.Attributes['sql'] );
               fSkeleton := VarToStr(  FileNode.Attributes['skeleton'] );
               FileNode := FileNode.NextSibling;
            end;
            XMLDoc.Active := False;
         finally
            XMLDoc := Nil;
         end;
      end;
    
      procedure TReportDefItem.SetFileName(const Value: string);
      begin
         if fFileName <> Value
         then begin
            fFileName := Value;
            Load;
         end;
      end;
      end.
    

    Use as :

    fReports := TReportDefList.Create( TReportDefItem );
    fReports.LoadList();
    

提交回复
热议问题