重现步骤:
-新的 firemonkey-应用程序
-将 TCheckBox 添加到表单
现在使用以下代码创建一个方法:
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;
使用 lVal 的转换运行没有问题,但是,即使 checkbox1.data 也是 TValue,在调用 AsInteger
时也会抛出“EInvalidCast”错误。 。使用checkbox1.data.AsType<Integer>
时出现同样的错误相反。
我是否滥用了TCheckBox.Data
是这里还是某种错误?
最佳答案
TCheckBox.Data
包含 boolean
值,特别是复选框选中状态。这就是为什么当您尝试从中读取 Integer
时会收到 EInvalidCast
错误。
您看似可以放入Integer
数据的原因是,TCheckBox.SetData
只会忽略无效的数据类型并将Data
设置为错误
。
关于delphi - TCheckBox.Data 用作 TValue 时类型转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30568375/