プロジェクトの途中でジェネリック TList を使用しているときに、数日前に問題が発生しました。簡単なテスト プロジェクトでテストしたところ、同じ問題が発生しました。正確なコードは次のとおりです。
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;
Label1.Caption は10ではなく100になります。なぜこのようになっているのですか?参照渡しではなく値渡しだと思いました!TList.Add(const value)