Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:34:29+00:00 2026-05-22T15:34:29+00:00

I encountered a problem some days ago while working with Generic TList in the

  • 0

I encountered a problem some days ago while working with Generic TList in the middle of a project. I tested it in a simple test project and got the same problem. Here is the exact code:

type
  TMyPoint = record
    x: Integer;
    y: Integer;
  end;

  TShape = record
    Corners: TList<TMyPoint>;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    Shape_List: TList<TShape>;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Shape: TShape;
  FirstPoint: TMyPoint;
  SecondPoint: TMyPoint;
  Temp: TMyPoint;
begin
  // Create the corners list
  Shape.Corners := TList<TMyPoint>.Create;
  // Add the first point to corners
  FirstPoint.x := 10;
  FirstPoint.y := 20;
  Shape.Corners.Add(FirstPoint);
  // Add the shape to the Shape_List
  Shape_List.Add(Shape);

  // Clear the shape corners
  Shape.Corners.Clear;

  // Add another point to corners
  SecondPoint.x := 100;
  SecondPoint.y := 200;
  Shape.Corners.Add(SecondPoint);

  // Show the x of the first point of the first shape
  Label1.Caption := IntToStr(Shape_List[0].Corners[0].x);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Shape_List := TList<TShape>.Create;
end;

The Label1.Caption will be 100 not 10 ! Why is it like this? I thought TList.Add(const value) is pass-by-value not pass-by-reference!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:34:30+00:00Added an answer on May 22, 2026 at 3:34 pm

    Added some comments to your procedure to point out exactly where the “bug” is.

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Shape: TShape;
      FirstPoint: TMyPoint;
      SecondPoint: TMyPoint;
      Temp: TMyPoint;
    begin
      // Create the corners list
      Shape.Corners := TList<TMyPoint>.Create; // We create a new TList<TMyPOint> OBJECT. Shape.Corners is a reference
      // Add the first point to corners
      FirstPoint.x := 10;
      FirstPoint.y := 20;
      Shape.Corners.Add(FirstPoint); // Add FirstPoint to the list we created at step 1.
      // Add the shape to the Shape_List
      Shape_List.Add(Shape); // Add a copy of the Shape record to the Shape_List
    
      // Clear the shape corners
      Shape.Corners.Clear; // Clear the Shape.Corners reference. This effectively clears the list of corners
                           // in the Shape you just added to Shape_List because it contains the same
                           // reference.
    
      // Add another point to corners
      SecondPoint.x := 100;
      SecondPoint.y := 200;
      Shape.Corners.Add(SecondPoint); // Add a new point to the Corners list. Remamber, Corners is actually
                                      // a reference. The first Shape you added to Shape_List contains a copy
                                      // of this exact same reference, so you're effectively adding a first point
                                      // to both Shape.Corners and Shape_List[0].Corners.
    
      // Show the x of the first point of the first shape
      Label1.Caption := IntToStr(Shape_List[0].Corners[0].x); // Yup, you just added that point, so you get 100
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered a problem when running some old code that was handed down to
I've encountered a problem while writing code in Delphi. Namely I can't get acces
We encountered a problem with using Subversion on Windows. A developer committed a file
I recently encountered a problem where a value was null if accessed with Request.Form
I've encountered a problem when retrieving a JSONP response from a server in a
I've encountered a problem with my Netbeans 6.1 IDE. After an unsuccessful update, Netbeans
I have encountered a problem that I have not come accross yet when setting
Uhm I'm not sure if anyone has encountered this problem a brief description is
I'm migrating a TSQL stored procedure to PL/SQL and have encountered a problem -
I am using ajaxForm. Now I have encountered a problem. My idea is when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.