[lazarus] Setting a value using RTTI
Michael Van Canneyt
michael.vancanneyt at wisa.be
Fri Dec 1 08:44:39 EST 2000
On Fri, 1 Dec 2000, Shane Miller wrote:
> Question for all:
>
> Using RTTI howdo I set a property value?
> In my source code for TComponentInterface.SetPropbyName
> I get the PPropinfo record for the property VISIBLE and
> I check the Setproc variable. It's <> nil so I need
> to call it somehow. I have a TMySetProc type that is a
>
> Procedure (const value) of Object;
>
> In TComponentInterface I have a variable MySetProc of type TMySetProc. I assign MySetProc by....
>
> MySetProc := TMySetProc(SetProc^);
>
> then I call it
>
> MySetProc(Value);
>
> It crashes. Thoughts?
The correct way to do this is using setordprop:
procedure SetOrdProp(Instance : TObject;PropInfo : PPropInfo; Value : Longint);
You could do like this;
// Easy access function:
function GetPropInfo(Instance: TObject; const PropName: string): PPropInfo;
begin
Result := GetPropInfo(Instance.ClassType, PropName);
end;
Actual code:
begin
SetOrdProp(Instance,GetPropInfo(Instance),Ord(Value))
end;
Only use the SetXXXprop functions to set properties, don't start looking in
the propinfo records.
I will enhance the typinfo unit ASAP, so functions of the kind
procedure SetSetProp(Instance: TObject; const PropName: string;const Value: string);
are available. They were introduced in Delphi 5 as well; you could then
use these functions.
Michael.
More information about the Lazarus
mailing list