在TObjectList中存储和读取对象

时间:2016-09-16 13:26:15

标签: oop delphi delphi-7

    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;

我使用Delphi 7并尝试存储和显示测量结果。但是如果我尝试读取X / Y点,我会得到一个范围异常。如果我评论FLine.Clear;,它就可以了。你是如何解决这个问题的?

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

所有GetItem-函数看起来像:

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

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您应该在DoMeasuring中创建FLine

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;