Recent

Author Topic: Creating an array of class methods  (Read 2883 times)

lelanthran

  • Newbie
  • Posts: 2
Creating an array of class methods
« on: March 24, 2019, 09:24:38 pm »
Hello all,

This is my first (or second) post here so please forgive any breaches of etiquette I make.

I'm trying to create an array to store all my callbacks for TToolButton clicks. While it appears (i.e. the compiler didn't complain) that I can create a type for the callbacks like this:

Code: Pascal  [Select][+][-]
  1. type
  2.    TTabExplorer = class (TTabSheet)
  3.    // ... Class declaration goes here
  4.    end;
  5.  
  6.    TToolBtnFunc = procedure (Sender: TObject);
  7.    TToolBtnClick = array [0..11] of TToolBtnFunc;
  8.  

the compiler complains when I populate TToolBtnClick instances with TTabExplorer methods because the actual functions are methods of the class TTabExplorer. How can I change the snippet
Code: Pascal  [Select][+][-]
  1. type
  2.    TToolBtnFunc = procedure (Sender: TObject);
  3.    TToolBtnClick = array [0..11] of TToolBtnFunc;
  4.  

to one that lets TToolBtnClick hold the type TTabExplorer.methodname (Sender: TObject);

Warm Regards
Lee

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: Creating an array of class methods
« Reply #1 on: March 24, 2019, 09:45:41 pm »
Maybe this
Code: Pascal  [Select][+][-]
  1. TToolBtnFunc = procedure (Sender: TObject) of object;

lelanthran

  • Newbie
  • Posts: 2
Re: Creating an array of class methods
« Reply #2 on: March 24, 2019, 09:57:11 pm »
Maybe this
Code: Pascal  [Select][+][-]
  1. TToolBtnFunc = procedure (Sender: TObject) of object;

Hello

Firstly, thank you for your help.

Unfortunately that does not work as I am trying to populate the array at compile-time  - the compiler errors out with the message:

uttabexplorer.pas(73,4) Error: Typed constants of the type "procedure of object" can only be initialized with NIL


(Of course, it's entirely possible that I am doing something wrong with the of object keywords)

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Creating an array of class methods
« Reply #3 on: March 24, 2019, 11:02:30 pm »
Unfortunately that does not work as I am trying to populate the array at compile-time  - the compiler errors out with the message:

uttabexplorer.pas(73,4) Error: Typed constants of the type "procedure of object" can only be initialized with NIL

sorry that not possible. can alone do runtime.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyClass = class(TObject)
  3.       procedure Click(Sender: TObject);
  4.   end;
  5.  
  6.   TToolBtnFunc = procedure (Sender: TObject) of object;
  7.   TToolBtnClick = array [0..1] of TToolBtnFunc;
  8.  
  9. var
  10.   MyClass : TMyClass;
  11.   ClickArray : TToolBtnClick = (nil, nil);
  12.  
  13. procedure TMyClass.Click(Sender: TObject);
  14. begin
  15. end;
  16.  
  17. begin
  18.   ClickArray[0] := @MyClass.Click;     
  19. end.
  20.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Creating an array of class methods
« Reply #4 on: March 25, 2019, 12:11:58 am »
The attached example project illustrates one way to implement your idea.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Creating an array of class methods
« Reply #5 on: March 25, 2019, 01:36:31 am »
Or use a TtoolBar and get it over with  :D
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018