Monday, October 23, 2023

delphi - How to work with generic TArray

Using generic TArray with String type.
procedure TFDrawingRelease.doReading;
var
  f : TextFile;
  sRow : string;
  aSplitted: TArray<string>;
begin
  { -- delete all rows }

  if QData.Active then
    begin
      QData.Close;
      QData.Open;
    end;

  { -- open file and read data (two columns divided by ";") }

  AssignFile( f, EFileName.Text );
  try
    Screen.Cursor := crHourGlass;
    QData.DisableControls;

    reset( f );  
    
    while not eof( f ) do
      begin
        readln( f, sRow );

        aSplitted := sRow.Split( [';'], 2 );

        QData.Insert;
        { ..and return to first char _ }
        QDataItemid.AsString := aSplitted[0].Split( [ '_' ], 1 )[0];
        QDataName.AsString := aSplitted[1];
        QData.Post;
      end;
  finally
    closeFile( f );

    QData.EnableControls;
    Screen.Cursor := crDefault;
  end;

  SetButtons;
end;