Does anyone know why this Lazarus / Pascal code gives error?

0

It is a simple class with get and set methods, but it generates an error when invoking any of the functions or procedures (from the beginning I found that only on line 43).

unit uPais;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Dialogs;

type
  Pais = class
    private
      codigo:integer;
      descricao:string;
    public
      constructor Create; // construtor
      destructor  Destroy; // destrutor
      // Setters
      procedure setCodigo(pCodigo:integer);
      procedure setDescricao(pDescricao:string);
      // Getters
      function getCodigo():integer;
      function getDescricao():string;

  end;// fim da classe

implementation
constructor Pais.Create();
begin
  codigo := 0;
  descricao := '';
end;

destructor Pais.Destroy();
begin
  //Freemem(@codigo);
  //Freemem(@descricao);
end;

// Setters
procedure Pais.setCodigo(pCodigo:integer);
begin
  codigo := pCodigo;
end;

procedure Pais.setDescricao(pDescricao:string);
begin
  descricao := pDescricao;
end;

// Getters
function Pais.getCodigo():integer;
begin
  Result := codigo;
end;

function Pais.getDescricao():string;
begin
  Result := descricao;
end;

end.
    
asked by anonymous 01.08.2016 / 19:22

1 answer

0

In Object Pascal, the correct thing is to use property , like this:

unit uPais;

{$mode objfpc}{$H+}

interface

uses Classes, SysUtils, Dialogs;

type
    Pais = class
    private
      fCodigo: Integer; // por convenção insira um "f" antes do nome do campo
      fDescricao: string;

      procedure SetCodigo(Value: Integer);
      procedure SetDescricao(Value: string);
    public
        constructor Create; // construtor
        destructor  Destroy; // destrutor

        property Codigo Integer read fCodigo write SetCodigo;
        property Descricao string read fDescricao write SetDescricao;

    end;// fim da classe

implementation

constructor Pais.Create;
begin
    fCodigo    := 0;
    fDescricao := '';
end;

destructor Pais.Destroy;
begin
    //Freemem(@codigo);
    //Freemem(@descricao);
end;

// Setters
procedure Pais.SetCodigo(Value: Integer);
begin
    fCodigo := Value;
end;

procedure Pais.SetDescricao(Value: string);
begin
    fDescricao := Value;
end;

end.

If you want, create getters in the same way.

    
01.08.2016 / 20:04