You cannot rely on a constructor because, contrary to classes, records are not required for use, the default constructor without parameters is used implicitly.
But you can use a constant field:
type TPacket = record type TBytish = 0..250; const InitByte : Byte = 255; var FirstVal, SecondVal: TBytish; end;
Then use this as a regular record, except that you do not (and cannot) change the InitByte
field.
FillChar
maintains a constant field and behaves as expected.
procedure TForm2.FormCreate(Sender: TObject); var r: TPacket; begin FillChar(r, SizeOf(r), #0); ShowMessage(Format('InitByte = %d, FirstVal = %d, SecondVal = %d', [r.InitByte, r.FirstVal,r.SecondVal]));
Updated to include a range of nested type 0..250
source share