Recent

Author Topic: Initialize Form Variable?  (Read 1031 times)

TfUG2Zt6tFyqUG

  • Newbie
  • Posts: 3
Initialize Form Variable?
« on: October 04, 2023, 10:17:56 am »
Hello all, I'm sure this is a simple question but I can't seem to find an answer.  I can find documentation on initializing variables in general, but the syntax doesn't seem to work for my code.

I'm going through the "simple editor" tutorial and wanted to adjust the label for changes to show 0 instead of 1.

Below is what I'd _like_ to do, but it doesn't compile with the initialization to 0.

Code: Pascal  [Select][+][-]
  1. private
  2.     fMemoChanges: Integer = 0;
  3.  

How do I accomplish this?

cdbc

  • Hero Member
  • *****
  • Posts: 722
    • http://www.cdbc.dk
Re: Initialize Form Variable?
« Reply #1 on: October 04, 2023, 11:17:43 am »
Hi
I haven't got the foggiest...
But... In fpc and lazarus, if you create a class, all the memberfields are initialized to 0/nil/'' etc. in "InitInstance". If you desire a variable/memberfield to have another value on startup, you can do so in the class' constructor, usually called "Create". In TForm regi, you'd fill in an event-handler, namely "TForm1.FormCreate" like this:
Code: Pascal  [Select][+][-]
  1. TForm1 = class(TForm)
  2.   ...
  3. private
  4.   fMemoChanges: integer; // NO '= 0;'
  5.   ...
  6. end;
  7.  
  8. implementation
  9.  
  10. TForm1.FormCreate(Sender: TObject);
  11. begin
  12.   // here fMemoChanges has been initialized to 0 beforehand...
  13.   fMemoChanges:= 4711;
  14. end;
  15.  
Hth
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11080
  • FPC developer.
Re: Initialize Form Variable?
« Reply #2 on: October 04, 2023, 11:25:49 am »
cdbc is right, Pascal requires you to write the code.

TfUG2Zt6tFyqUG

  • Newbie
  • Posts: 3
Re: Initialize Form Variable?
« Reply #3 on: October 05, 2023, 10:24:24 am »
Thanks everyone, that got me going.

Another question if I may.

What is the following syntax doing exactly?

specifically, why the { TForm1 }
and then how do the properties, such as btnOpen relate to TForm1 = class(TForm)?

It seems like the type is just a type with a class in it and the properties are part of the type rather than the class?

Pascal syntax is very new to me so I'm unsure how to interpret this.

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TForm1 }
  4.  
  5.   TForm1 = class(TForm)
  6.     btnOpen: TButton;
  7.     btnSave: TButton;
  8.     btnFont: TButton;
  9.     btnNew: TButton;
  10.     chkDetails: TCheckBox;
  11.     FontDialog1: TFontDialog;
  12.     lblCursorPos: TLabel;
  13.     lblPanelStatus: TLabel;
  14.     lblMemoChanges: TLabel;
  15.     Memo1: TMemo;
  16.     OpenDialog1: TOpenDialog;
  17.     pnlStatus: TPanel;
  18.     SaveDialog1: TSaveDialog;
  19.     procedure btnFontClick(Sender: TObject);
  20.     procedure btnNewClick(Sender: TObject);
  21.     procedure btnOpenClick(Sender: TObject);
  22.     procedure btnSaveClick(Sender: TObject);
  23.     procedure Memo1Change(Sender: TObject);
  24.  

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1719
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Initialize Form Variable?
« Reply #4 on: October 05, 2023, 10:36:00 am »
The connection rely inside the .lfm file.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

af0815

  • Hero Member
  • *****
  • Posts: 1265
Re: Initialize Form Variable?
« Reply #5 on: October 05, 2023, 10:53:04 am »
As CodeZwerg says, but don't use the file lfm-file direct, it is maintained by Lazarus itself.
regards
Andreas

Lansdowne

  • New Member
  • *
  • Posts: 23
Re: Initialize Form Variable?
« Reply #6 on: October 05, 2023, 11:38:41 am »
The 
{ TForm1 }
is just a comment.  You can ignore it.

Code: Pascal  [Select][+][-]
  1. Type
  2. TForm1 = class(TForm)
  3. ...
  4. ...
  5. end;

means

"I am declaring a type of object based on the standard class TForm and it has the following properties:"

And of course
Code: Pascal  [Select][+][-]
  1. var
  2. Form1: TForm1;

means

"I am declaring one form of this type"

And finally there is probably
Form1=TForm1.Create;
or equivalent in your project file.

cdbc

  • Hero Member
  • *****
  • Posts: 722
    • http://www.cdbc.dk
Re: Initialize Form Variable?
« Reply #7 on: October 05, 2023, 12:04:21 pm »
Hi
Regarding
Quote
And finally there is probably
Form1=TForm1.Create;
or equivalent in your project file.
Here's an excerpt from a fresh project-file:
Code: Pascal  [Select][+][-]
  1. program empty;
  2.  
  3. {$mode objfpc}{$H+} // sets up compiler to use long ansistrings and use
  4.                      // classes and interfaces, the whole shebang ~ framework
  5.  
  6. uses
  7.   {$IFDEF UNIX} // if you are on linux
  8.   cthreads,  // include support for threading
  9.   {$ENDIF}
  10.   {$IFDEF HASAMIGA} // for amiga too
  11.   athreads,
  12.   {$ENDIF}
  13.   Interfaces, // this includes the LCL widgetset
  14.   Forms, umain // the components enabling you to show a form on screen
  15.   { you can add units after this };
  16.  
  17. {$R *.res} // include the form resource, how to draw the form
  18.  
  19. begin
  20.   RequireDerivedFormResource:=True; // use included resource
  21.   Application.Scaled:=True;  // helps in scaling proportions on screen
  22.   Application.Initialize; // startup the app-mechanics ~ messageloop etc.
  23.   Application.CreateForm(TForm1, Form1); // create the form you see
  24.   Application.Run; // starts the app, receiving communication from OS/you
  25. end. // you end up here when you close your form
  26.  
This project stub, sets the whole framework it takes to run an application, in motion  :D
As you can see, most of it is happening inside the type "TApplication", of which "TForm" is a member, of which your labels are members/properties... Etc. %)
Regards Benny
« Last Edit: October 05, 2023, 12:08:39 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6

TfUG2Zt6tFyqUG

  • Newbie
  • Posts: 3
Re: Initialize Form Variable?
« Reply #8 on: October 06, 2023, 10:26:28 am »
Got it, thanks guys.

I learn best by asking lots of questions so please bear with me :)

as I'm looking at it now, I think part of what threw me is a combination of the syntax being so different and the indentation of the end; statement.


One could also express it as

type TForm1 = class(TForm)
.
.
.
private
.
.
.
public
.
.
.
end;

where class(TForm1) ... end; is a block.

So really the way I should be reading this file is as a type declaration, a single variable declaration (global?), and then an implementation of the type.  Can the implementation section and the type declaration be in different files, similar to C/C++ header vs source file?

reading through the lpm file, it appears this is "main", one of the things it's doing is loading the "Unit1" unit, where that's defined in the Unit1.pas.  And the reason Unit1.pas has the variable declaration (var Form1: TForm1;) is specifically to create a "global" that the lpm file can use when creating the form [Application.CreateForm(TForm1, Form1);]

Why does TForm1 need to be specified in the Application.CreateForm call if the type of Form1 is already TForm1?

In the lfm file, this appears to be a listing of property overrides for the lazarus form view.  One thing that I'm struggling to understand is this file has the syntax [object Form1: TForm1], whereas the pas file has the syntax (type Form1=class(TForm1)]

Can someone help me understand what's going on here?

---

I'm reading through this documentation:
https://wiki.freepascal.org/Object_Oriented_Programming_with_Free_Pascal_and_Lazarus

1 statement confuses me a bit

> Note that this class is defined as an instance of another parent or ancestor class (TFPCustomPen) from which it inherits all its properties and methods. It has some fields of its own, grouped under

does instance in this case mean derived class?  To me "instance" and "inherits" don't really go together unless this is prototype based inheritance similar to javascript.

also, in the following example

Code: Pascal  [Select][+][-]
  1. TFooClass = class
  2.   private
  3.     FIntProp: Integer;
  4.   public
  5.     property IntProp: Integer read FIntProp write FIntProp;
  6.   end;
  7.  

Im guessing but for FIntProp the property keyword is implied?  So the only time you really need to explicitly specify something as a property is if you're going to use additional property specifiers such as read/write?

And lastly, the index property specifier.  Is this just a built-in way of building custom "array" types?  I'm trying to understand the use case for this and that's all I can really think of.


cdbc

  • Hero Member
  • *****
  • Posts: 722
    • http://www.cdbc.dk
Re: Initialize Form Variable?
« Reply #9 on: October 06, 2023, 12:23:11 pm »
Hi
Right, I'll start off, trying to answer your questions, I'm sure our forum-mates will chime in  ;)
1)
Quote
where class(TForm1) ... end; is a block.
   Yes, technically => "Form1 = class(TForm1) ... end; is a block."
2)
Quote
So really the way I should be reading this file is as a type declaration, a single variable declaration (global?), and then an implementation of the type.  Can the implementation section and the type declaration be in different files, similar to C/C++ header vs source file?
   Yes to the first part and no to splitting declaration & implementation of a
   class. One wrinkle, you can use include-files, but I'll save that for a later
   round of questions...
3)
Quote
And the reason Unit1.pas has the variable declaration (var Form1: TForm1;) is specifically to create a "global" that the lpr file can use when creating the form [Application.CreateForm(TForm1, Form1);]
   Yes, that's how it works.
4)
Quote
Why does TForm1 need to be specified in the Application.CreateForm call if the type of Form1 is already TForm1?
   "Application.CreateForm" is a form Factory, it can create any form you'd
   like, it just needs to know the type to create.
5)
Quote
One thing that I'm struggling to understand is this file has the syntax [object Form1: TForm1], whereas the pas file has the syntax (type Form1=class(TForm1)]
   Well, look at the extensions... the *.pas file is written in ...Tadaa Pascal
   and the *.lfm file is written in (L)azarus (F)orm (M)etadata, an object-
   describing meta-language, that describes object -properties & -relations
   on the form, used in loading/streaming, the syntax is kinda like xml.
6)
Quote
does instance in this case mean derived class?
   When you do: "TForm1 = class(TForm)", you create a new Descendant
   class of TForm that inherits all of TForm's properties, methods and abilities
   to load from file. Then you add your own props and methods to that new
   type. An instance is:
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1; // <-- Form1 is a concrete "Instance" of TForm1
7)
Quote
Im guessing but for FIntProp the property keyword is implied?  So the only time you really need to explicitly specify something as a property is if you're going to use additional property specifiers such as read/write?
   Well, it's about visibility, outside of "Unit1.pas" you can't see or touch
   "FIntProp", it's [private] to the class and the unit, in which it's declared,
   hence you need to surface it as a property in the [public] section, for the
   outside world to use it. And yes, this way you can use getter/setter
   -methods for the property, which in turn can have sideeffects, e.g.:
   Setting TList.Sorted to true, will trigger the TList.sort method.
8)
Quote
And lastly, the index property specifier.  Is this just a built-in way of building custom "array" types?  I'm trying to understand the use case for this and that's all I can really think of.
   Yes this is <also> what they are used for, but you can have a look at
   "TStringList.Names[]" and "TStringList.Values[]", they're nifty ways of using
   indexed properties  8-)
Pheeewww, HTH, have fun  :D
Regards Benny
« Last Edit: October 06, 2023, 12:30:05 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6

 

TinyPortal © 2005-2018