Search code examples
delphifieldrtti

Use RTTIField.SetValue to set a Pointer to a Field


I have a Type:

TControlPointer = ^TControl;

And a private field in a class:

TMyClass = class(TObject)
  private
  FPointer : TControlPointer;
end;

When a I try to set the value for FPointer using RTTI:

procedure SetControlPointer(pControl : TControlPointer);
[...]
RTTIField.SetValue(Self,pControl);

Compiler says [dcc32 Error] myunit.pas (xxx): E2010 Incompatible types: 'TValue' and 'TControlPointer'

Any ideas?

Thank you


Solution

  • The error is simply telling you that RTTIField.SetValue expects to be passed a TValue. So I guess you just need to put the pointer into a TValue

    TValue.From<TControlPointer>(Self.pControl)
    

    That's what you pass to RTTIField.SetValue.

    You can probably let the compiler infer the type and simply write

    TValue.From(Self.pControl)