Access To Form lazarus / 31-LCL ---------------------------------------------------------------------- Weitentaaal · 2021-02-18 10:28 hello I didn't get the Principe of this: I Have a Form and create an instance of it (ex. Form1 : TForm1) i Wrote a Pretty Long Function, in wich i need to access some Form objects. Something like this: Form1.Edit1 but i can just add TForm1.Procedure/Function to the Signature. So why should i use this :Form1.Edit1 ? when i easly could add the class Name to the Signature every Time. can Some1 pls Tell me what add TForm1.---- does to the Function/Procedure ? Thanks a Lot LG Loch ---------------------------------------------------------------------- wp · 2021-02-18 11:43 Several possibilities: To be specific I assume some calculation task: calculate the area of a rectangle. Width and Height of the rectangle are set in two edit controls (Edit1 and Edit2) on Form1 /1/ Make the calculation a method of the form (i.e. declare it inside the form's definition). Then the form knows what Edit1 and Edit2 are, and you can (to be exact: MUST) drop the form variable. 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   TForm1 = class(TForm)    Edit1: TEdit;    Edit2: TEdit;     procedure CalcArea;    ...  end; function TForm1.CalcArea: Double;var  w, h: Double;begin  w := StrToFloat(Edit1.Text);  h := StrToFloat(Edit2.Text);  Result := w*h;end;  /2/ Alternatively, write a standalone function, ideally in a separate unit and add parameters for the edit values. (If there are many parameters you can consider defining a special record for the parameters, or write a special class for doing the calculation in which you can set the calculation parameters by means of properties). The calling function must replace these parameters by the values of Edit1 and Edit2 (see below): 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 function CalcArea(AWidth, AHeight: Double): Double;begin  Result := AWidth * AHeight;end; /3/ Write a separate function but refer to the edit controls now without parameters: 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 function CalcArea: Double;begin  Result := StrToFloat(Form1.Edit1.Text) * StrToFloat(Form1.Edit2.Text); Please don't do this. It makes your calculation dependent on controls of a form; it assumes that the form is called Form1, but if, later during development, you decide to rename the form to CalcAreaForm, suddenly your code will not compile any more. Or suppose the calculation is very complex and your boss tells you to write a unit test for it - this is a test program separate from your application. When CalcArea has its own parameter list you can simply pass parameters to the testing routine, but when the parameters refer to Form1 you suddenly must pull the application' form units into the test program. Always try to keep logic and calculations separate from the forms in a way that these units can be used independently. In principle these arguments hold also for variant /1/. I still would stick to /1/ for such a simple calculation as in this example, but a very complex calculating definitely should be kept in a separate unit leading again to case /2/ as the best solution. Now agreening that case /2/ is the way to go, how is this function invoked? There are two cases: At first, let's assume that this function is called by a click on a button in Form1. Form1 is an instance of the class TForm1 which contains the Edit1 and Edit2 controls. All instances of a class "know" about the contained  controls - there is no need to mention Form1. In fact, this would be very wrong because then the code will work only when the currently used instance is called exactly like that: "Form1" 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.CalculateButtonClick(Sender: TObject);var  area: Double;begin  area := CalcArea(StrToFloat(Edit1.Text), StrToFloat(Edit2.Text));  // do not specify "Form1"  ShowMessage(Format('The area is: %f', [area]));end; In the other case the CalcArea function is called by a click on a button on a form belonging to some other class which does not inherit from TForm1. Now the two edit boxes are not known (in fact, there may even be equally named edit controls in this form too!), and you must fully specify their names by specifying the form on which they are - in our simple case this is Form1, but in general the form may also be named like "OtherForm"... 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 TForm2.CalculateButtonClick(Sender: TObject);var  area: Double;begin  area := CalcArea(StrToFloat(Form1.Edit1.Text), StrToFloat(Form1.Edit2.Text));   ShowMessage(Format('The area is: %f', [area]));end; ---------------------------------------------------------------------- MarkMLl · 2021-02-18 11:50 Because encapsulation and access control are generally considered to be fundamental principles of good programming. You should be able to find a general grounding in that elsewhere. If you declare MyFirstEditRoutinne() to be part of TForm1, then (a) you can control whether it can be called by procedures outside TForm1 and/or outside the unit in which TForm1 is declared, (b) you can give it access to state stored in the context of TForm1 (i.e. in the "strict private" section) and (c) you can store its state inside TForm1. Point (c) is particularly important if for some reason TForm1 is instantiated multiple times: each TForm1.MyFirstEditRoutinne() would have its own state. MarkMLl ---------------------------------------------------------------------- Weitentaaal · 2021-02-18 13:09 Much thanks For your detailed Reply's ---------------------------------------------------------------------- engkin · 2021-02-18 13:39 Quote from: Weitentaaal on February 18, 2021, 11:28:33 amcan Some1 pls Tell me what add TForm1.---- does to the Function/Procedure ? The compiler generates a function/procedure with a hidden first parameter of type TForm1: 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 function YourFunction(Self: TForm1; ... ---------------------------------------------------------------------- Martin_fr · 2021-02-18 15:30 If you are new to object orientated programming, or new to OO in Pascal, then maybe this will help: http://www.delphibasics.co.uk/Article.asp?Name=OO I have not myself read that particular article, but the site offers generally good help. As I said, I did not read the article, but I expect that it (or one of the ones linked from it) will explain the diff between procedure Foo; procedure TForm1.Foo; (or TBar.Foo) If you are just looking for how to easily add new methods to your TForm, then this will help: https://wiki.lazarus.freepascal.org/Lazarus_IDE_Tools#Class_Completion I do recommend to read the entire wiki page. The IDE has a lot of nice functions to help you. I also recommend to read the below, and discover even more nice IDE functions. https://wiki.lazarus.freepascal.org/New_IDE_features_since You may not need them all today, but knowing they exist, means you can start using them when you need.