Accessing child class from main form lazarus / 21-General ---------------------------------------------------------------------- nikel · 2025-01-03 05:31 Hello, I followed the tutorial at this link: https://www.tutorialspoint.com/pascal/pascal_classes.htm I created this class: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Answers; {$mode ObjFPC}{$H+}{$m+} interface uses  {$IFDEF UNIX}  CThreads,  {$ENDIF}  Classes, SysUtils; type  TAnswers = class  protected    Num           : Byte;    UserAnswer    : Char;  public  constructor Create(N: Byte; U: Char);     procedure SetNum(N: Byte);  function GetNum(): Byte;     procedure SetUserAnswer(U: Char);    function GetUserAnswer(): Char;  end; type  TCorrectAnswer = class(TAnswers)  private    CorrectAnswer    : Char;  public    constructor Create(N: Byte); overload;    constructor Create(N: Byte; C: Char; U: Char); overload;     procedure SetCorrectAnswer(C: Char);    function GetCorrectAnswer(): Char;  end; implementation constructor TAnswers.Create(N: Byte; U: Char);begin  Num:=N;  UserAnswer:=U;end; procedure TAnswers.SetNum(N: Byte);begin  Num:=N;end; function TAnswers.GetNum(): Byte;begin  GetNum:=Num;end; procedure TAnswers.SetUserAnswer(U: Char);begin  UserAnswer:=U;end; function TAnswers.GetUserAnswer(): Char;begin  GetUserAnswer:=UserAnswer;end; constructor TCorrectAnswer.Create(N: Byte);begin  inherited Create(N, #0);  CorrectAnswer:=#0;end; constructor TCorrectAnswer.Create(N: Byte; C: Char; U: Char);begin  inherited Create(N, U);  CorrectAnswer:=C;end; procedure TCorrectAnswer.SetCorrectAnswer(C: Char);begin  CorrectAnswer:=C;end; function TCorrectAnswer.GetCorrectAnswer(): Char;begin  GetCorrectAnswer:=CorrectAnswer;end; begin end. How can I access SetCorrectAnswer and GetCorrectAnswer from my main form? ---------------------------------------------------------------------- egsuh · 2025-01-03 08:46 The class definition is simply type definition. So, you need a variable that will instantiate the class. So, in your main form, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,   Answers;             // you need to include this unit in the form unit type   TForm1 = class(TForm)      Button1: TButton;      procedure Button1Click(Sender: TObject);   private    public    end; var   Form1: TForm1;   CorrectAnswer: TCorrectAnswer;  implementation {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);begin    CorrectAnswer := TCorrectAnswer.create(0);   // instantiate    try        CorrectAnswer.SetCorrectAnswer('C');                 // call functions & procedures of CorrectAnswer, not TCorrectAnswer        ShowMessage(CorrectAnswer.GetCorrectAnswer);    finally        CorrectAnswer.Free;   end; end; end.  And you may define a property like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TCorrectAnswer = class(TAnswers)  private    FCorrectAnswer    : Char;   //  This is customary, so as to use CorrectAnswer as a public property     procedure SetCorrectAnswer(C: Char);    function GetCorrectAnswer(): Char;  public    constructor Create(N: Byte); overload;    constructor Create(N: Byte; C: Char; U: Char); overload;     property CorrectAnswer : Char read GetCorrectAnswer write SetCorrectAnswer;   end; implementation procedure TCorrectAnswer.SetCorrectAnswer(C: Char);begin  FCorrectAnswer:=C;end; function TCorrectAnswer.GetCorrectAnswer: Char;begin    Result := FCorrectAnswer;end;   And with this definition, you can write in your main form unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin    CorrectAnswer := TCorrectAnswer.create(0);   // instantiate    try        CorrectAnswer.CorrectAnswer := 'C';                 // this will call setCorrectAnswer        ShowMessage(CorrectAnswer.CorrectAnswer);   // this wil call getCorrectAnswer   finally        CorrectAnswer.Free;   end; end; ---------------------------------------------------------------------- Joanna · 2025-01-03 11:21 Another small detail.. it should not be possible to change the value of c Parameter inside of of the procedure SetCorrectAnswer. kodezwerg was nice enough to teach me this back in 2019  thanks kodezwerg  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   procedure SetCorrectAnswer(Const C: Char); ---------------------------------------------------------------------- Handoko · 2025-01-04 08:53 Quote from: egsuh on January 03, 2025, 09:46:45 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin    CorrectAnswer := TCorrectAnswer.create(0);   // instantiate    try        CorrectAnswer.CorrectAnswer := 'C';                 // this will call setCorrectAnswer        ShowMessage(CorrectAnswer.CorrectAnswer);   // this wil call getCorrectAnswer   finally        CorrectAnswer.Free;   end; end; I personally think the try-finally block is not needed in the code above. I do not against the usage of exception and try blocks, I know sometimes they are needed. I always avoid using them when there is no good reasons of using them. I read from a game programming article, it said abusing exception block will degenerate the performance. Any expert here, please explain. Is Try-Finally block really needed in the code above? Should it be removed or not? And why? Please share your thought. ---------------------------------------------------------------------- Thaddy · 2025-01-04 09:05 You can remove it. https://wiki.freepascal.org/Avoiding_implicit_try_finally_section Goes for any managed type. Implicit exceptions are the default. You should only set them manually if $implicitexceptions are off. ---------------------------------------------------------------------- PascalDragon · 2025-01-06 10:47 Quote from: Handoko on January 04, 2025, 09:53:50 amQuote from: egsuh on January 03, 2025, 09:46:45 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin    CorrectAnswer := TCorrectAnswer.create(0);   // instantiate    try        CorrectAnswer.CorrectAnswer := 'C';                 // this will call setCorrectAnswer        ShowMessage(CorrectAnswer.CorrectAnswer);   // this wil call getCorrectAnswer   finally        CorrectAnswer.Free;   end; end; I personally think the try-finally block is not needed in the code above. I do not against the usage of exception and try blocks, I know sometimes they are needed. I always avoid using them when there is no good reasons of using them. I read from a game programming article, it said abusing exception block will degenerate the performance. Any expert here, please explain. Is Try-Finally block really needed in the code above? Should it be removed or not? And why? Please share your thought. If an exception occurs either in the setter of CorrectAnswer the getter of CorrectAnswer or the call of ShowMessage then you'll have a memory leak, because no one will free the CorrectAnswer instance. try … finally is named a resource protection block for a reason, so use it. ---------------------------------------------------------------------- Thaddy · 2025-01-06 15:00 So then why is it documented that inside the routine there is an implicit try/finally? Should we duplicate that anyway? ---------------------------------------------------------------------- egsuh · 2025-01-06 15:49 QuoteSo then why is it documented that inside the routine there is an implicit try/finally? Should we duplicate that anyway? The document says we don't have to explicitly finalize AnsiString, Dynamic Array, etc.  So we can omit try and finally in following example, as well as "finalize" itself (thankfully). var      arr : array of integer; begin      SetLength (arr, 100);      try          //  Operations that might raise exceptions      finally          Finalize (arr);      end; end;       We simply write: var      arr : array of integer; begin      SetLength (arr, 100);      //  operations that might raise exceptions end;       AFAIK string type seem similars to integer, real, or char type (what are they called?) but in reality it is more like dynamic array in internal operations. ---------------------------------------------------------------------- BrunoK · 2025-01-06 16:07 Frankly, does one need a try finally on such trivial code ? ---------------------------------------------------------------------- PascalDragon · 2025-01-09 20:16 Quote from: Thaddy on January 06, 2025, 04:00:55 pmSo then why is it documented that inside the routine there is an implicit try/finally? Should we duplicate that anyway? The implicit try … finally only protects managed variables used inside the function, not manually instantiated object instances, so yes, one needs to duplicate them. Quote from: BrunoK on January 06, 2025, 05:07:40 pmFrankly, does one need a try finally on such trivial code ? If you can be sure that neither the property getter/setter nor the ShowMessage will raise an exception then (and only then) you can leave out the explicit try … finally. However even then I would add it nevertheless to be future proof in case these assumption ever change.