Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set NULL to Variant field using rtti

Tags:

null

delphi

rtti

I have a problem setting null to a variant field using rtti.

Suppose I have class like this:

TClass1 = class
  Field1:Integer;  
  Field2:Variant; 
end;

when I get access to a Field1 I can do:

Field.SetValue(TObject(ValueObject.AsObject), 1);

but it seems I can not do this with Field2:

Field.SetValue(TObject(ValueObject.AsObject), null);

or

Field.SetValue(ValueObject.AsObject, TValue.FromVariant(NULL));

So, what I want is to be able to set null value to a Field2 of class above. Is it possible? Any ideas would be appreciated.

Goran

like image 781
gorann Avatar asked Feb 05 '10 11:02

gorann


2 Answers

This works:

Field.SetValue(Self, TValue.From<Variant>(Null) );
like image 198
Ville Krumlinde Avatar answered Sep 30 '22 04:09

Ville Krumlinde


This looks to be another manifestation of the bug I found while answering this question. SetValue doesn't handle "empty" values correctly for certain data types. I'll update the QC report with the new case. So, for the moment, you can't, at least not without patching the RTTI unit yourself.

like image 36
Mason Wheeler Avatar answered Sep 30 '22 04:09

Mason Wheeler