Recent

Author Topic: Records with methods  (Read 723 times)

reb

  • New member
  • *
  • Posts: 8
Records with methods
« on: July 22, 2019, 01:38:43 pm »
I am trying to create a custom record to store location of a point in 2d space.
The following code fails to compile:

Code: Pascal  [Select][+][-]
  1.   type
  2.     TVertex = record
  3.       x: Single;
  4.       y: Single;
  5.     public
  6.       procedure Assign(x_val, y_val: Single); // Fatal: Syntax error, ":" expected but "PROCEDURE" found
  7.     end;
  8.  

The definition of TVertex follows the same structure as the TPoint record declaration.

Code: Pascal  [Select][+][-]
  1. TPoint  =  packed record
  2.        x : Longint; y : Longint;
  3.      public
  4.        constructor Create(ax,ay:Longint); overload;
  5. end;  
  6.  

What am I doing wrong here?
« Last Edit: July 22, 2019, 01:41:40 pm by reb »

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Records with methods
« Reply #1 on: July 22, 2019, 01:44:58 pm »
Code: Pascal  [Select][+][-]
  1. {$mode delphi} // or: {$mode objfpc}{$modeswitch advancedrecords}
  2.   type
  3.     TVertex = record
  4.       x: Single;
  5.       y: Single;
  6.     public
  7.       procedure Assign(x_val, y_val: Single);
  8.     end;
  9.    
  10.      procedure TVertex.Assign(x_val, y_val: Single);
  11.      begin
  12.      end;
  13. begin
  14. end.
« Last Edit: July 22, 2019, 01:47:02 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Records with methods
« Reply #2 on: July 22, 2019, 01:47:26 pm »
or in ObjFpc mode missing at top of unit
Code: Pascal  [Select][+][-]
  1. {$MODESWITCH ADVANCEDRECORDS}

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Records with methods
« Reply #3 on: July 22, 2019, 01:50:07 pm »
What does my comment say?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

reb

  • New member
  • *
  • Posts: 8
Re: Records with methods
« Reply #4 on: July 22, 2019, 01:54:32 pm »
Code: Pascal  [Select][+][-]
  1. {$mode delphi} // or: {$mode objfpc}{$modeswitch advancedrecords}
  2.   type
  3.     TVertex = record
  4.       x: Single;
  5.       y: Single;
  6.     public
  7.       procedure Assign(x_val, y_val: Single);
  8.     end;
  9.    
  10.      procedure TVertex.Assign(x_val, y_val: Single);
  11.      begin
  12.      end;
  13. begin
  14. end.

Thank you! Adding it after unit worked!
{$mode objfpc}{$H+} {$MODESWITCH ADVANCEDRECORDS}

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Records with methods
« Reply #5 on: July 22, 2019, 02:01:24 pm »
What does my comment say?
Well I was writing my comment while your where editing yours to complete it.

Last Edit: Today at 01:47:02 pm by Thaddy

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Records with methods
« Reply #6 on: July 22, 2019, 02:04:06 pm »
I am trying to create a custom record to store location of a point in 2d space.
The following code fails to compile:

https://www.freepascal.org/docs-html/rtl/types/tpointf.html

reb

  • New member
  • *
  • Posts: 8
Re: Records with methods
« Reply #7 on: July 22, 2019, 02:09:54 pm »
I am trying to create a custom record to store location of a point in 2d space.
The following code fails to compile:

https://www.freepascal.org/docs-html/rtl/types/tpointf.html

This will be very useful, thanks :)
New to Lazarus, a lot to learn.

 

TinyPortal © 2005-2018