[fpc-pascal] Re: Class const of array of record.

Everton Vieira tonvieira at gmail.com
Fri Feb 24 17:29:13 CET 2012


Doesn't work this code doesn't compile:

type
  { TRec }
  TRec = record
    Nome: String;
    class function Create(aNome: String): TRec; static; inline;
  end;

procedure ShowTRec(rec: TRec);

implementation

{$R *.lfm}

procedure ShowTRec(rec: TRec);
begin
  ShowMessage(rec.Nome);
end;

{ TRec }

class function TRec.Create(aNome: String): TRec;
begin
  Result.Nome := aNome;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowTRec(TRec.Create('Pascal'));
end;

2012/2/24 kyan <alfasud.ti at gmail.com>

> >> Is there any how to do it?
>
> You can use advanced record syntax and do something like this:
>
> type
>  TRec = record
>    Numero: Integer;
>    Nome: String;
>     class function Create(ANumero: Integer; const ANome: string):
> TRec; static; inline;
>  end;
>
> ...
>
> class function TRec.Create(ANumero: Integer; const ANome: string): TRec;
> begin
>  with Result do
>  begin
>    Numero := ANumero;
>    Nome := ANome;
>  end;
> end;
>
> ...
>
> procedure TForm1.Button1Click(Sender: TObject);
> begin
>  ShowTRec(TRec.Create(1, 'Pascal'));
> end;
>
> PS: In Delphi advanced records can have constructors but not yet in
> FPC. A static class function can easily replace them.
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



-- 
Everton Vieira.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20120224/a7f7133c/attachment.html>


More information about the fpc-pascal mailing list