Use RTTIField.SetValue to set a Pointer to a Field

375 views Asked by At

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

1

There are 1 answers

6
David Heffernan On BEST ANSWER

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)