### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

#### pcurtis

• Hero Member
• Posts: 510
##### [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
Laz 2.0.12
FPC 3.2.0

#### JdeHaan

• Jr. Member
• Posts: 61
##### Re: Compare types
« Reply #1 on: February 28, 2021, 12:03:44 pm »
Code: Pascal  [Select][+][-]
1. program project1;
2. {\$mode objfpc}{\$H+}
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: 510
##### 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
Laz 2.0.12
FPC 3.2.0

#### GetMem

• Hero Member
• Posts: 4000
##### 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: 510
##### Re: [SOLVED] Compare types
« Reply #4 on: February 28, 2021, 02:50:42 pm »
Noted
Windows 10
Laz 2.0.12
FPC 3.2.0

#### jamie

• Hero Member
• Posts: 4366
##### Re: [SOLVED] Compare types
« Reply #5 on: February 28, 2021, 03:06:58 pm »

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