Lazarus

Programming => General => Topic started by: pcurtis on February 28, 2021, 11:12:48 am

Title: [SOLVED] Compare types
Post by: pcurtis on February 28, 2021, 11:12:48 am
Hi All,
how can I compare two types. e.g.

Code: Pascal  [Select][+][-]
  1. type
  2.   TGridposition = record
  3.     X : integer;
  4.     y : integer;
  5.   end;
  6.  
  7. var
  8.   NEWCELL : TGridposition;
  9.   OLDCELL : TGridposition;
  10.  
  11. if NEWCELL <> OLDCELL ..... FAIILS
  12.  
Title: Re: Compare types
Post by: JdeHaan on February 28, 2021, 12:03:44 pm
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$H+}
  3. {$ModeSwitch advancedrecords}
  4.  
  5. type
  6.   TGridposition = record
  7.     X : integer;
  8.     y : integer;
  9.     class operator =(left, right: TGridposition): Boolean;
  10.   end;
  11.  
  12. var
  13.   NEWCELL : TGridposition;
  14.   OLDCELL : TGridposition;
  15.  
  16. { TGridposition }
  17.  
  18. class operator TGridposition.=(left, right: TGridposition): Boolean;
  19. begin
  20.   Result := (left.X = right.X) and (left.y = right.y);
  21. end;
  22.  
  23. begin
  24.   OLDCELL.X := 10; OLDCELL.y := 100;
  25.   NEWCELL.X := 10; NEWCELL.y := 100;
  26.  
  27.   if NEWCELL = OLDCELL then
  28.     writeln('compare: true')
  29.   else
  30.     writeln('compare: false')
  31. end.
  32.  
  33.  
Title: Re: Compare types
Post by: pcurtis on February 28, 2021, 12:24:38 pm
Thanks - Seems a bit long winded.
Title: Re: [SOLVED] Compare types
Post by: GetMem on February 28, 2021, 01:18:04 pm
@pcurtis

TGridposition is basically a TPoint,  you can compare it using the PointsEqual api from LCLIntf:
Code: Pascal  [Select][+][-]
  1. uses LCLIntf;
  2.  
  3. var
  4.    P1, P2: TPoint;
  5. begin
  6.   P1.X := 0; P1.Y := 100;
  7.   P2.X := 0; P2.Y := 100;
  8.   if PointsEqual(P1, P2) then
  9.     ShowMessage('equal');
  10. end
Title: Re: [SOLVED] Compare types
Post by: pcurtis on February 28, 2021, 02:50:42 pm
Noted
Title: Re: [SOLVED] Compare types
Post by: jamie on February 28, 2021, 03:06:58 pm
To help you out a bit.

MyPoint := TPoint.create(x,y);

so use a TPOINT for your define instead... Its the same thing but it has all the operators and sub functions in it.

TGridPosition := TPoint;


if you are making some sort of compound record then include a Tpoint within ..

MyGridPos.Location := TMyGridPos.Location.Create(x,y);

etc

I am sure that should compile, I have D sitting in front of me atm and could test that if it does not.

TinyPortal © 2005-2018