How to use pointers in delphi?

1

I have the following code snippet:

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

I look for a menu item and check if it is visible. But 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 do FindComponent all the time?

    
asked by anonymous 18.07.2018 / 22:19

1 answer

3

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

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