Search code examples
oopdelphidelphi-7

Store and read the Objects in/from TObjectList


    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;

I work with Delphi 7 and I try to store and display the results of a measurement. But if I try to read the X / Y points I get a range exception. If I comment FLine.Clear; off, it works. How do you get around this problem?

In addition, the program comes out with a message that relates to the storage.

All of the GetItem- functions look like:

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

Thank you in advance.


Solution

  • You should create your FLine in DoMeasuring

    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;