Recent

Author Topic: Using protected procedures in descendants  (Read 529 times)

Derz

  • New Member
  • *
  • Posts: 15
Using protected procedures in descendants
« on: February 02, 2023, 09:12:21 pm »
I hope this is possible, but I haven't been able to get it to function.  I want to create a descendant of tStringGrid, for the purpose of exposing some if it's protected procedures.  I Created a descendant class, tMyStringGrid, but get compile errors when I try to invoke the parent's protected method...  (Only class methods.. may be accessed in class methods)

The code I started with is below.    Any suggestions?

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyStringGrid = class(TStringGrid)
  3.   public
  4. //    Procedure DoPasteFromClipboard; override;  (Just to test I can override the method)
  5.     Procedure PasteFromClipboard;
  6.  
  7.   end;
  8.  
  9. implementation
  10.  
  11. Procedure TMyStringGrid.PasteFromClipboard;
  12. Begin
  13.   TMyStringGrid.DoPasteFromClipboard;  // This line is where the compiler errors
  14. end;        
  15.  

Blaazen

  • Hero Member
  • *****
  • Posts: 3155
  • POKE 54296,15
    • Eye-Candy Controls
Re: Using protected procedures in descendants
« Reply #1 on: February 02, 2023, 09:27:49 pm »
Remove the TMyStringGrid:
Code: Pascal  [Select][+][-]
  1. Procedure TMyStringGrid.PasteFromClipboard;
  2. Begin
  3.   DoPasteFromClipboard;  // This line is where the compiler errors
  4. end;
 
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Derz

  • New Member
  • *
  • Posts: 15
Re: Using protected procedures in descendants
« Reply #2 on: February 02, 2023, 11:49:47 pm »
Thank you Blaazen.  That worked. I thought I had tried that but..

The rules about when to specify a class are unknown to me, so it is more trial, error and hope.  If there is a good reference..
I'll wait for a response to that then close this thread.

Blaazen

  • Hero Member
  • *****
  • Posts: 3155
  • POKE 54296,15
    • Eye-Candy Controls
Re: Using protected procedures in descendants
« Reply #3 on: February 03, 2023, 12:14:17 am »
I don't know any tutorial, but look at this code:
Code: Pascal  [Select][+][-]
  1. type
  2.   { TDemo }
  3.   TDemo = class
  4.     procedure Method;
  5.     class procedure ClassMethod;
  6.   end;
  7.  
  8. {$R *.res}
  9.  
  10. { TDemo }
  11.  
  12. class procedure TDemo.ClassMethod;
  13. begin
  14.   //no need to refer TDemo here
  15. end;
  16.  
  17. procedure TDemo.Method;
  18. begin
  19.   //no need to refer TDemo here
  20. end;
  21.  
  22. var Demo: TDemo;
  23. begin
  24.   TDemo.ClassMethod;
  25.   Demo:=TDemo.Create;
  26.   Demo.Method;
  27.   Demo.Free;
  28. end.
You can see the difference, i.e. you don't need instance of class if you call class method.
Anyway, you don't need to refer class name (here TDemo) inside implementation of its (class) methods.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

PascalDragon

  • Hero Member
  • *****
  • Posts: 4963
  • Compiler Developer
Re: Using protected procedures in descendants
« Reply #4 on: February 03, 2023, 03:48:55 pm »
The rules about when to specify a class are unknown to me, so it is more trial, error and hope.  If there is a good reference..

You only need to specify the class name when you want to invoke a class method (class procedure or class function), especially when done from outside the class (inside the class you don't need to specify the class name), when you access a class variable or property (again, only from outside the class, not inside it) and when you invoke a constructor to create a new instance.

Derz

  • New Member
  • *
  • Posts: 15
Re: Using protected procedures in descendants
« Reply #5 on: February 03, 2023, 05:23:43 pm »
Thank you PascalDragon for the explanation.

 

TinyPortal © 2005-2018