Recent

Author Topic: [SOLVED] Compare types  (Read 988 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Compare types
« 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.  
« Last Edit: February 28, 2021, 12:39:34 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

JdeHaan

  • Full Member
  • ***
  • Posts: 115
Re: Compare types
« Reply #1 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.  

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Compare types
« Reply #2 on: February 28, 2021, 12:24:38 pm »
Thanks - Seems a bit long winded.
« Last Edit: February 28, 2021, 12:40:50 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

balazsszekely

  • Guest
Re: [SOLVED] Compare types
« Reply #3 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

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: [SOLVED] Compare types
« Reply #4 on: February 28, 2021, 02:50:42 pm »
Noted
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: [SOLVED] Compare types
« Reply #5 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.

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018