Recent

Author Topic: TStringList Assigments(SOLVED)  (Read 512 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
TStringList Assigments(SOLVED)
« on: August 20, 2022, 04:22:45 am »
I have 2 tstringlist in a program and  I'm trying to assign AList to BList but can't figure out the syntax. BList:=Alist not working. BList.Lines:=AList.Lines didn't work.

AList.lines.Assign(BList); Not working;

Is there a way?

Thanks.
« Last Edit: August 20, 2022, 05:01:59 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dje

  • Full Member
  • ***
  • Posts: 134
Re: TStringList Assigments
« Reply #1 on: August 20, 2022, 04:47:23 am »
To Assign AList to BList use:
Code: Pascal  [Select][+][-]
  1. BList.Assign(AList);    

Edit: The above will also copy various TStrings properties. If you just want to copy the contents use:
Code: Pascal  [Select][+][-]
  1. BList.SetStrings(AList);

or, To append AList to BList use:
Code: Pascal  [Select][+][-]
  1. BList.AddStrings(AList);
« Last Edit: August 20, 2022, 04:52:49 am by derek.john.evans »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: TStringList Assigments
« Reply #2 on: August 20, 2022, 04:52:48 am »
I have 2 tstringlist in a program and  I'm trying to assign AList to BList but can't figure out the syntax. BList:=Alist not working. BList.Lines:=AList.Lines didn't work.

AList.lines.Assign(BList); Not working;

Is there a way?

Thanks.
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.   AList, BList :TStringlist;
  4.   Failed :Boolean;
  5. begin
  6.   AList := TStringList.Create;
  7.   BList := TStringList.Create;
  8.   try
  9.     AList.Add('String 1');
  10.     AList.Add('String 2');
  11.     AList.Add('String 3');
  12.     AList.Add('String 4');
  13.     AList.Add('String 10');
  14.     AList.Add('String 11');
  15.     AList.Add('String 12');
  16.     AList.Add('String 13');
  17.     BList.Assign(AList);
  18.     if AList.Count <> BList.Count then ShowMessage('Assignment Failed');
  19.     if AList.Strings[2] <> BList.Strings[2] then Failed := True;
  20.     if AList.Strings[5] <> BList.Strings[5] then Failed := True;
  21.     if AList.Strings[6] <> BList.Strings[6] then Failed := True;
  22.     if Failed then
  23.       ShowMessage('Assignment Failed');
  24.     else
  25.       ShowMessage('Assignment Succided');
  26.   finally
  27.     AList.Free;
  28.     BList.Free;
  29.   end;
  30. end;
  31.  

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringList Assigments
« Reply #3 on: August 20, 2022, 05:01:34 am »
Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

simone

  • Hero Member
  • *****
  • Posts: 573
Re: TStringList Assigments(SOLVED)
« Reply #4 on: August 20, 2022, 01:32:36 pm »
If you are interested in copying only the textual content, without the other properties, you can also do this:

Code: Pascal  [Select][+][-]
  1. BList.Text: = AList.Text;
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: TStringList Assigments(SOLVED)
« Reply #5 on: August 20, 2022, 01:36:52 pm »
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses classes;
  3. var
  4.   AList, BList :TStrings;
  5. begin
  6.   AList := TStringList.Create;
  7.   BList := TStringList.Create;
  8.   try
  9.     AList.AddStrings(['String 1','String 2','String 3','String 4','String 10','String 11', 'String 12']);
  10.     BList.Assign(AList);
  11.     // all if's are nonsense. The only exception that can occur is EOutOfMemory, which needs a try/except.
  12.     writeln('Assignment Succeeded');
  13.   finally
  14.     BList.Free;  // creation order is reversed on free.
  15.     AList.Free;
  16.   end;
  17. end.
https://www.freepascal.org/docs-html/rtl/classes/tstrings.create.html
« Last Edit: August 20, 2022, 01:44:51 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018