user3140961
user3140961

Reputation:

Delphi TBytesField SetData

In a table, I have a TBytesField with size 60. When a record is created with this field set to nil, I want to set its value to an array of bytes like [0,0,0.....0]. I am using this code:

var
  ar : array of byte;
begin
  if ByteField.AsBytes = nil then
  begin
    SetLength(ar, ByteField.Size);
    ByteField.SetData(ar, True);
  end;
end;

Although ar has all bytes zero, I see that the field takes these values instead:

[17,32,0,0,0,0,0,0,48,192,182,1,0...0]

What am I doing wrong?

Upvotes: 2

Views: 866

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596176

Don't use SetData() directly. Use the AsBytes property:

var
  ar : TBytes;
begin
  if ByteField.IsNull then
  begin
    SetLength(ar, ByteField.Size);
    ByteField.AsBytes := ar;
  end;
end;

Upvotes: 2

Related Questions