Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/RTL/Delphi/Other RTL    [ Add a report in this area ]  
Report #:  104609   Status: Reported
Stringlist assignment to itselft clears content
Project:  Delphi Build #:  XE, XE2
Version:    16.4 Submitted By:   Christian Holzner
Report Type:  Minor failure / Design problem Date Reported:  4/4/2012 1:24:49 AM
Severity:    Infrequently encountered problem Last Updated: 4/4/2012 11:26:31 PM
Platform:    All versions Internal Tracking #:  
Resolution: None  Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
An assignment of a TStringlist to itself clears its contents. Same behavior with a TList.

[System.Classes.pas]
------------
...
procedure TStrings.Assign(Source: TPersistent);
begin
  if Source is TStrings then
  begin
    BeginUpdate;
    try
      Clear;
...
------------
Steps to Reproduce:
Execute the following code and see that after the "SL.Assign(SL2)" the stringlist(s) are empty.

------------
var
  SL: TStringList;
  SL2: TStringList;
begin
  SL := TStringList.Create;
  try
    SL2 := SL;
    SL2.Add('Foo');
    ShowMessage(Format('SL.Count: %d;  SL2.Count: %d', [SL.Count, SL2.Count]));
    SL.Assign(SL2);          // Clears the stringlist.   "SL.Text := SL2.Text;" would work
    ShowMessage(Format('SL.Count: %d;  SL2.Count: %d', [SL.Count, SL2.Count]));
  finally
    SL.Free;
  end;
------------
Workarounds
Assign to a copy of the stringlist.
Attachment
None
Comments

None

Server Response from: ETNACODE01