FranzB 0 Posted November 28, 2020 (edited) I want to use this class with an array of TPoint type. type TedgearryPoints = Array [1..4] of TPoint ; type TGEORect = class ..... private : Fedges : TedgearryPoints ; public : ..... property edges : TedgearryPoints read Fedge write Fedge; end; but the code fails because I can not assign data to the individual edges MyGEORECT.Edges[1] , compiles says "[dcc64 Error] GEOUnit.pas(1303): E2064 Left side cannot be assigned to " Edited November 28, 2020 by FranzB Share this post Link to post
FPiette 383 Posted November 28, 2020 You have to use an indexed property: property Edges[Index : Integer] : TPoint read GetEdges write SetEdges; And then implement GetEdges and SetEdges: procedure TGeoRect.SetEdges(Index : Integer; Pt : TPoint); begin if (Index < Low(FEdges)) or (Index > High(FEdges)) then raise ERangeException.Create('Invlid index'); FEdges[Index] := Pt; end; You may also use a type for the range used for indexing the array. GetEdge is similar. Share this post Link to post
FranzB 0 Posted November 28, 2020 (edited) but this means : I have to write extra code if I want to assign just one edge with your solution above via property or if I want to assign all 4 edge with one command making the edge variable public: allows both, but I think from class design this look not that good type TGEORect = class private : ..... public : edges : TedgearryPoints ; end; Edited November 28, 2020 by FranzB Share this post Link to post
FPiette 383 Posted November 28, 2020 32 minutes ago, FranzB said: if I want to assign all 4 edge with one command I would create an overloaded SetEdges: procedure TGEORect.SetEdges(const AEdges : TEdgeArrayPoints); begin FEdges := AEdges; end; Share this post Link to post
Guest Posted November 29, 2020 12 hours ago, FranzB said: I want to use this class with an array of TPoint type. ... MyGEORECT.Edges[1] , compiles says "[dcc64 Error] GEOUnit.pas(1303): E2064 Left side cannot be assigned to " try this way: unit for Class: unit uMyGEORectClass; interface uses System.Types, System.SysUtils; type TEdgeArryPoints = array [0 .. 3] of TPoint; // TGEORect = class private FMyEdges: TEdgeArryPoints; procedure SetFMyEdges(const Value: TEdgeArryPoints); function GetFMyEdgesPoints: TArray<TPoint>; public constructor Create(Aowner: TObject); destructor Destroy; // property MyEdges: TEdgeArryPoints read FMyEdges write SetFMyEdges; property MyEdgesPoints: TArray<TPoint> read GetFMyEdgesPoints; end; implementation { TGEORect } constructor TGEORect.Create(Aowner: TObject); var i: integer; begin inherited Create; // for i := low(FMyEdges) to high(FMyEdges) do FMyEdges[i] := TPoint.Zero; end; destructor TGEORect.Destroy; begin inherited Destroy; end; function TGEORect.GetFMyEdgesPoints: TArray<TPoint>; var lMyPoint: TPoint; begin for lMyPoint in FMyEdges do result := result + [lMyPoint]; end; procedure TGEORect.SetFMyEdges(const Value: TEdgeArryPoints); begin FMyEdges := Value; end; end. Using the new Class: implementation {$R *.dfm} uses uMyGEORectClass; var lMyTEdgeArryPoints: TEdgeArryPoints; lMyTGEORect : TGEORect = nil; procedure TForm1.btn_Creating_PointsClick(Sender: TObject); var i: integer; begin if not(lMyTGEORect = nil) then begin Randomize; // to Random() below! // for i := low(lMyTEdgeArryPoints) to high(lMyTEdgeArryPoints) do lMyTEdgeArryPoints[i] := TPoint.Create(Random(200), Random(400)); // lMyTGEORect.MyEdges := lMyTEdgeArryPoints; // // lMyTGEORect.MyEdges[0].SetLocation(0,0); works for 1 item! end; end; procedure TForm1.btn_Showing_PointsClick(Sender: TObject); var lMyPoints: TPoint; lText : string; nPoint : integer; begin if not(lMyTGEORect = nil) then begin for lMyPoints in lMyTGEORect.MyEdgesPoints do lText := lText + Format('[%d,%d]', [lMyPoints.X, lMyPoints.Y]) + ','; // if lText.EndsWith(',') then lText := lText.Remove(lText.Length - 1); // Delete(lText,lText.Length,1); // ShowMessage(lText); // // ShowMessage(lMyTGEORect.MyEdges[0].X.ToString + ' - ' + lMyTGEORect.MyEdges[0].Y.ToString); end; end; procedure TForm1.FormCreate(Sender: TObject); begin lMyTGEORect := TGEORect.Create(nil); // btn_Creating_Points.Click; end; procedure TForm1.FormDestroy(Sender: TObject); begin if not(lMyTGEORect = nil) then lMyTGEORect.Free; end; initialization ReportMemoryLeaksOnShutdown := true; finalization end. NOTE: im not "builder" ok! hug Share this post Link to post
pmcgee 10 Posted November 29, 2020 (edited) Just checking ... You can characterise one edge by one TPoint? or one edge by 4 TPoints? or does Fedges describe a parallelogram? Is ok. "GEORect". I get it. (I'm not sure 'edges' is the best name) ... (also ... can't see how to delete posts.) Edited November 29, 2020 by pmcgee Share this post Link to post
Guest Posted November 29, 2020 (edited) 48 minutes ago, pmcgee said: Just checking ... You can characterise one edge by one TPoint? or one edge by 4 TPoints? or does Fedges describe a parallelogram? Is ok. "GEORect". I get it. (I'm not sure 'edges' is the best name) ... (also ... can't see how to delete posts.) // lMyTGEORect.MyEdges[0].SetLocation(0,0); works for 1 item! = NOT WORKS FOR YOU? lMyTGEORect.MyEdges[0].SetLocation(1,1); lMyTGEORect.MyEdges[1].SetLocation(2,2); lMyTGEORect.MyEdges[2].SetLocation(3,3); lMyTGEORect.MyEdges[3].SetLocation(4,4); the "names" stay with you! dont worry! if you needs many "TGEORect" or "MyEdges", you can extende the class to create a List of Objects with this type, you see? for example: I THINK THAT YOU NEED CHANGE SOMETHING YOUR CLASS BASE for better use! a simple way: uses uMyGEORectClass; var lMyTEdgeArryPoints: TEdgeArryPoints; lMyTGEORect : TGEORect = nil; // lManyTGEORect: array [0 .. 3] of TGEORect; ... procedure TForm1.FormCreate(Sender: TObject); var i: integer; begin for i := low(lManyTGEORect) to high(lManyTGEORect) do begin lManyTGEORect[i] := TGEORect.Create(nil); end; ... end; ... // you should use: // lManyTGEORect[ index n ].MyEdges[ index n ] to access the element ... procedure TForm1.FormDestroy(Sender: TObject); var i: integer; begin for i := low(lManyTGEORect) to high(lManyTGEORect) do begin if not(lManyTGEORect[i] = nil) then lManyTGEORect[i].Free; end; end; I think that is not possible "delete" the posts! Edited November 29, 2020 by Guest Share this post Link to post