How to use Delphi pointers?

Asked

Viewed 214 times

1

I have the following code snippet:

if TMenuItem(fmPrincipal.FindComponent('teste')) <> nil then         
  if TMenuItem(fmPrincipal.FindComponent('teste')).Visible then
  ...

I’m looking for a menu item and I’m checking to see if it’s visible. However my code is growing and I’m having to add more checks if the item is enabled etc.

How could I use a pointer to point to this object and use the pointer from then on, without having to keep doing the FindComponent all the time?

1 answer

3

Assigning it to a variable by reference. This way simplifies access and search quantity will be extremely reduced.

var 
  mi: TMenuItem;
begin
  mi := TMenuItem(fmPrincipal.FindComponent('teste'));
  if mi <> nil then
  begin
    if mi.Visible then
    ...
    mi.Enabled:= ...
  end;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.