Jack
Jack

Reputation: 67

How use Procedure with Record using Delphi 7?

What's wrong if this code, I just want to put a procedure inside of the record:

unit unTEFTipos;

interface

type

  TTEFPagamento = record
    AcrescimoDesconto: Double;
    TrocoSaque: Double;
    procedure Clear;
  end;

implementation

procedure TTEFPagamento.Clear;
begin
  AcrescimoDesconto := 0;
  TrocoSaque := 0;
end;

end.

But the Delphi 7 IDE is returning this errors:

 Build

 [Error] unTEFTipos.pas(10): 'END' expected but 'PROCEDURE' found  
 [Error] unTEFTipos.pas(11): 'IMPLEMENTATION' expected but ';' found  
 [Error] unTEFTipos.pas(13): '.' expected but 'IMPLEMENTATION' found  
 [Error] unTEFTipos.pas(10): Unsatisfied forward or external declaration: 'Clear'  
 [Fatal Error] GP.dpr(486): Could not compile used unit 'TEF\unTEFTipos.pas'

Upvotes: 0

Views: 1633

Answers (1)

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43023

In older versions of Delphi, you have to use object instead of record if you want to add methods.

 TTEFPagamento = object
    AcrescimoDesconto: Double;
    TrocoSaque: Double;
    procedure Clear;
  end;

It will be compatible with newer versions too, even if you may face some problems when initializing managed variables within it.

So that I finally end up with writing something like:

 TTEFPagamento = {$ifdef UNICODE}record{$else}object{$endif}
    AcrescimoDesconto: Double;
    TrocoSaque: Double;
    procedure Clear;
  end;

which compiles on all versions, and behave the same.

Upvotes: 3

Related Questions