Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamical assignment of values in Delphi

Tags:

delphi

rtti

I am creating a control similar to object inspector , So i want to assign any changes to the property to the relevant object.

var

v:TValue ;
 ctx : TRttiContext;
begin

  //  k.IsOrdinal := true ;
v := v.FromVariant(2)  ;


ctx.GetType(tButton).GetProperty('Style').SetValue(Button1, v.AsOrdinal);

end;

above is my code , but i am getting invalid type cast error.

Is it possible to handle any variable and enums .(No need of objects and records as it is very complicated )

like image 873
VibeeshanRC Avatar asked Dec 16 '25 15:12

VibeeshanRC


1 Answers

The call to SetValue needs to read like this:

SetValue(Button1, TValue.From(TButton.TButtonStyle(2)))

In your code, the use of AsOrdinal is incorrect. That is a function that returns a TRttiOrdinalType. But TRttiOrdinalType is described thus:

TRttiOrdinalType is the class used to describe all the Delphi ordinal value types, such as Integer, Byte, Word, and so on.

But you need to provide a TValue that represents a TButtonStyle, which is what the code above achieves.


As an aside, I initially tried to use the generic TValue.From<T>() function like this:

SetValue(Button1, TValue.From<TButton.TButtonStyle>(TButton.TButtonStyle(2)));

But that just resulted in the following internal compiler error:

[DCC Fatal Error] Unit58.pas(38): F2084 Internal Error: URW1147

QC#103129

Every time I attempt to use generics I end up being defeated by these internal errors!

Thanks to Serg for pointing out the alternative form of calling the parameterised method using type inference does not fall foul of the internal error.

like image 61
David Heffernan Avatar answered Dec 19 '25 05:12

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!