Recent

Author Topic: ObjectBinaryToText fails when Variant property = Null  (Read 345 times)

dmitryb

  • New Member
  • *
  • Posts: 46
ObjectBinaryToText fails when Variant property = Null
« on: May 31, 2020, 02:12:39 am »
Looks like ObjectBinaryToText doesn't work when Variant property have Null value.

This code fails in Lazarus but work perfectly well in Delphi.

Code: Pascal  [Select][+][-]
  1. type
  2.   TTestClass = class(TComponent)
  3.   private
  4.     FValue: Variant;
  5.   public
  6.   published
  7.     property Value: Variant read FValue write FValue;
  8.   end;
  9.  
  10.  
  11. procedure TForm2.Button1Click(Sender: TObject);
  12. var
  13.   ms: TMemoryStream;
  14.   ss: TStringStream;
  15.   tc: TTestClass;
  16. begin
  17.   tc := TTestClass.Create(nil);
  18.   tc.Value := Null;
  19.  
  20.   ms := TMemoryStream.Create;
  21.  
  22.   ms.WriteComponent(tc);
  23.  
  24.   ms.Position := 0;
  25.  
  26.   ss := TStringStream.Create('');
  27.   ObjectBinaryToText(ms, ss);
  28.  
  29.   Memo1.Text := ss.DataString;
  30.  
  31.   ss.Free;
  32.   ms.Free;
  33.   tc.Free;
  34. end;
  35.  

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: ObjectBinaryToText fails when Variant property = Null
« Reply #1 on: May 31, 2020, 02:59:30 am »
Hmm, well If I use a #0 as the null like you would expect in AnsiStrings, it works fine.

tc.Value := #0;

So I don't know where the real problem is, maybe the function NULL has the wrong return type ?
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 2435
  • Compiler Developer
Re: ObjectBinaryToText fails when Variant property = Null
« Reply #2 on: May 31, 2020, 11:03:38 am »
Looks like ObjectBinaryToText doesn't work when Variant property have Null value.

This code fails in Lazarus but work perfectly well in Delphi.

Code: Pascal  [Select][+][-]
  1. type
  2.   TTestClass = class(TComponent)
  3.   private
  4.     FValue: Variant;
  5.   public
  6.   published
  7.     property Value: Variant read FValue write FValue;
  8.   end;
  9.  
  10.  
  11. procedure TForm2.Button1Click(Sender: TObject);
  12. var
  13.   ms: TMemoryStream;
  14.   ss: TStringStream;
  15.   tc: TTestClass;
  16. begin
  17.   tc := TTestClass.Create(nil);
  18.   tc.Value := Null;
  19.  
  20.   ms := TMemoryStream.Create;
  21.  
  22.   ms.WriteComponent(tc);
  23.  
  24.   ms.Position := 0;
  25.  
  26.   ss := TStringStream.Create('');
  27.   ObjectBinaryToText(ms, ss);
  28.  
  29.   Memo1.Text := ss.DataString;
  30.  
  31.   ss.Free;
  32.   ms.Free;
  33.   tc.Free;
  34. end;
  35.  

Please report with a small, self contained example that does not depend on the LCL that shows what the output is and what it should be.

 

TinyPortal © 2005-2018