I would like to know how do I declare a record, that has some fixed values. I need to send data using this pattern: Byte($FF)-Byte(0..250)-Byte(0..250). I am using record for that and I would like to have the first value of it constant, so that it can’t be messed up.
Such as:
TPacket = record
InitByte: byte; // =255, constant
FirstVal,
SecondVal: byte;
end;
You can’t rely on a constructor because, contrary to Classes, Records are not required to use them, the default parameterless constructor being used implicitly.
But you can use a constant field:
Then use this as a regular Record, except that you don’t have (and you can’t) change the
InitBytefield.FillCharpreserves the constant field and behaves as expected.Updated to include the nested type range
0..250