delphifiremonkeyrttitvalue

TCheckBox.Data invalid typecast when using as TValue


Steps to reproduce:

-new firemonkey-application

-add TCheckBox to form

Now create a method with following code:

procedure TForm1.Foo;
var a,b:Integer; lVal:TValue;
begin
  lVal:=TValue.From<Integer>(42);
  a:=lVal.AsInteger;
  checkbox1.Data:=TValue.From<Integer>(42);
  b:=checkbox1.Data.AsInteger;
end;   

The cast with lVal runs without problems, checkbox1.data however, even if it's a TValue too, throws an "EInvalidCast"-Error, when calling AsInteger. The same error appears when using checkbox1.data.AsType<Integer> instead.

Am I misusing TCheckBox.Data here or is this some kind of bug?


Solution

  • TCheckBox.Data contains boolean value, specifically check box checked state. That is why you get EInvalidCast error when you try to read Integer from it.

    The reason why you seemingly can put Integer data in, is because TCheckBox.SetData will just ignore invalid data types and sets Data to False.