delphirecorddelphi-6finalize

How to finalize record passed through the untyped parameter of a function?


Can I pass "any" record type to my procedure ?

Many times I used "records" with strings.

type 
  TR = record
    a: string;
    b: string;
  end;

To clear them, I need to write:

Finalize(R);
FillChar(R, SizeOf(R), #0);

The question is that how to I pass any kind of records to clear it ?

For this I got this hint: "Expression needs no initialize/finalize".

procedure ClearRecord(var R);
begin
  Finalize(R);
  FillChar(R, SizeOf(R), #0);
end;

Thanks for every info!


Solution

  • Do not make it overly complicated. If you don't want to write a two-liner to clear the record, consider declaring:

    Const TR_Empty: TR = ();
    

    and use it:

    R := TR_Empty;
    

    And as commented by others, a generic procedure to do this is not worth the effort.

    If you would have Delphi-2009 or newer, this code is valid for clearing a record:

    R := Default(TR);