Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 在TObjectList中存储和读取对象_Oop_Delphi_Delphi 7 - Fatal编程技术网

Oop 在TObjectList中存储和读取对象

Oop 在TObjectList中存储和读取对象,oop,delphi,delphi-7,Oop,Delphi,Delphi 7,我使用Delphi7,尝试存储和显示测量结果。但是如果我尝试读取X/Y点,我会得到一个范围异常。如果我评论FLine.Clear关闭,它可以工作。你如何避开这个问题 此外,该程序还会显示一条与存储器相关的消息 所有GetItem函数看起来都像: TPoint = Class private FX: double; FY: double; public constructor Create(X,Y:double);

我使用Delphi7,尝试存储和显示测量结果。但是如果我尝试读取X/Y点,我会得到一个范围异常。如果我评论
FLine.Clear关闭,它可以工作。你如何避开这个问题

此外,该程序还会显示一条与存储器相关的消息

所有GetItem函数看起来都像:

    TPoint = Class
      private
        FX: double;
        FY: double;
      public
        constructor Create(X,Y:double); 
        destructor Destroy; override;
        property X: double read FX write FX;
        property Y: double read FY write FY;
      end;

      TLine= Class(TObjectList)
      private
        function GetItem(Index: Integer): TPoint;
        procedure SetItem(Index: Integer; const Value: TPoint);
      public
        constructor Create;
        destructor Destroy; override;
        procedure Add(APoint:TPoint);
        property Items[Index: Integer]: TPoint read GetItem write SetItem; default;
      end;

      TLineStorage= Class(TObjectList)
        private
        function GetItem(Index: Integer): TLine;
        procedure SetItem(Index: Integer; const Value: TLine);
      public
        constructor Create;
        destructor Destroy; override;
        procedure Add(ALinie:TLine);
        property Items[Index: Integer]: TLine read GetItem write SetItem; default;
      end;

      TStorage= Class(TObjectList)
      private
        function GetItem(Index: Integer): TLineStorage;
        procedure SetItem(Index: Integer; const Value: TLineStorage);
      public
        constructor Create;
        destructor Destroy; override;
        procedure Add(ALinieStorage:TLineStorage);
        property Items[Index: Integer]: TLineStorage read GetItem write SetItem; default;
      end;

      TMeasuring= Class
      private
        FLine: TLine;
        FLineStorage: TLineStorage;
        FStorage: TStorage;
      public
        constructor Create;
        destructor Destroy; override;
        procedure DoMeasuring;
      end;

{ TMeasuring }

constructor TMeasuring.Create;
begin
    inherited;
    FLineStorage:= TLineStorage.Create;
  FLineStorage.OwnsObjects:= true;
  FLine:= TLine.Create;
  FLine.OwnsObjects:= true;
  FStorage:= TStorage.Create;
  FStorage.OwnsObjects:= true;
end;

destructor TMeasuring.Destroy;
begin
  FLineStorage.Free;
  FLine.Free;
  FStorage.Free;
  inherited;
end;

procedure TMeasuring.DoMeasuring;
var i,j: integer; X,Y: double;
begin
    for j:= 0 to 2 do    // Lines
  begin
    for i:= 0 to 5 do  // Points
    begin
      X:= 1+ random(100);
      Y:= 1+ random(100);
      FLine.Add(TPoint.Create(X,Y)); // 1... X Points
    end;
    FLineStorage.Add(FLine); // 3 x Linie
    FLine.Clear; // for new measuring !!! if not-> it works !!!
  end;

  FStorage.Add(FLineStorage);  // save

  for i:= 0 to FStorage.Count-1 do
  begin
    X:= FStorage.Items[i].Items[i].Items[i].X; // it doesn't work...
    ShowMessage(FloatToStr(X));
  end;

end;

提前感谢您。

您应该在DoMeasuring中创建您的FLine

function TLine.GetItem(Index: Integer): TPoint;
begin
    result:= TPoint(inherited Items[Index]);
end;

谢谢你的回答。它起作用了!但是我收到了StackOverflow的消息。我需要在哪里打电话给“FLine.Free”?线路将在FlineStorage销毁时释放。因为TobjectList“拥有”看起来很重的数据。为什么不使用简单的坐标记录?@DavidHeffernan谢谢你的建议。我已经考虑过并将把TPoint课程改成唱片。
procedure TMeasuring.DoMeasuring;
var i,j: integer; X,Y: double;
    FLine : TLine; 
begin
    for j:= 0 to 2 do    // Lines
  begin
    FLine := TLine.create;
    for i:= 0 to 5 do  // Points
    begin
      X:= 1+ random(100);
      Y:= 1+ random(100);
      FLine.Add(TPoint.Create(X,Y)); // 1... X Points
    end;
    FLineStorage.Add(FLine); // 3 x Linie
    //FLine.Clear; // for new measuring !!! if not-> it works !!!
  end;

  FStorage.Add(FLineStorage);  // save

  for i:= 0 to FStorage.Count-1 do
  begin
    X:= FStorage.Items[i].Items[i].Items[i].X; // it doesn't work...
    ShowMessage(FloatToStr(X));
  end;

end;