I would like to access components via pointers instead of searching using FindComponent. The following assignment works for components placed on the Form, but not for dynamically created components. For example:
var
Pbtn: ^TButton;
Button2: TButton;
begin
Pbtn := @Button1;
Showmessage(pbtn.caption); // works well
Button2 := TButton.Create(Form2);
Pbtn := @Button2;
Showmessage(pbtn.caption); // not work
...
CodePudding user response:
A class type is a reference type, so object instances of a class type are already represented as pointers. Don't use ^
/@
to refer to these objects (that will only refer to the pointers themselves, which is rarely ever needed in most situations), eg:
var
Pbtn: TButton;
Button2: TButton;
begin
Pbtn := Button1;
ShowMessage(pbtn.Caption); // works well
Button2 := TButton.Create(Form2);
Pbtn := Button2;
ShowMessage(pbtn.Caption); // also works well
...