Recent

Author Topic: [SOLVED] How to talk with parent ?  (Read 17137 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #15 on: December 06, 2012, 08:11:40 pm »
Humm, no, im are right, im totally stupid  :-X

I do not understand what will demonstrate the example, for me it should be simpler to do (direct access to variable):

Code: [Select]
type
  TCar = class
  public
    FColor: string;
    FBuildYear: integer;
    procedure SetColor(CarColor: string);
  public
    property Color: string read FColor write SetColor; //Reads directly from the FColor variable;
    // directs writes to the SetColor procedure that then changes the FColor variable.
    // Another option could be to just do "write FColor" to directly write to the FColor variable..
 
  end;

procedure PropertyExample();

  MyCar: TCar;               
begin
  MyCar := TCar.Create;       
  try
    MyCar.FColor:='Green';      // // here FColor (not Color)
    showmessage(MyCar.FColor);
  finally
    MyCar.Free;         
  end;
end;

Of course i miss something but it is the goal of the game : understand (nee ?)
« Last Edit: December 06, 2012, 08:15:16 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [SOLVED] How to talk with parent ?
« Reply #16 on: December 06, 2012, 08:29:27 pm »
I understand what you mean & no, you're not stupid ;)

BTW Official manual on objects/properties: http://www.freepascal.org/docs-html/ref/refsu30.html#x77-870006.4.1.. in case my explanations only confuse you ;)

Yes you are right: putting FColor in a public section as in your snippet would immediately give access.

Fortunately I updated the article to show you why that is not always a good idea: suppose we want to log every time the color is changed to a file, or suppose we want to determine a car's color on, say, what color most of the components are (bonnet, doors, roof, trunk, side panels etc)?
E.g. in the example in the article, the code warns you if you assign Pink as color. We could also validate/check for valid values (i.e. not allow 'Invisible' or empty strings)

If you just make FColor public, you can set FColor without going through SetColor, bypassing logging etc!

Does this help?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #17 on: December 06, 2012, 09:07:10 pm »
Quote
BTW Official manual on objects/properties: http://www.freepascal.org/docs-html/ref/refsu30.html#x77-870006.4.1.. in case my explanations only confuse you ;)

You gives me light but Official manual on objects/properties is, for me,  less clear than a explanation of void pointer of structured little medium endian variable in United Assembler/C+- .

 
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

mas steindorff

  • Hero Member
  • *****
  • Posts: 576
Re: [SOLVED] How to talk with parent ?
« Reply #18 on: December 06, 2012, 09:34:38 pm »
@bigChimp  I like the way you had it where you first describe read and write to a simple variable first. 
I suggest that you later (down the page) replace the direct read/write with a function/procedure to show variable synchronization  may provide a better description of property's abilities.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #19 on: December 06, 2012, 09:35:28 pm »
Sorry, i come back with the initial topic :

Quote
Quote

    testclass.ChildClass.Parent:=testclass; // You hadn't given parent any value

does the trick.

Maybe, but in the code there is :

Code: [Select]
constructor TChildClass.Create(CreateSuspended: Boolean;
    const StackSize: SizeUInt);
 begin
   inherited Create(CreateSuspended,StackSize);
    FreeOnTerminate := True;
    Parent:=FParent;  <<<<< here assign Parent
   end;

And (help BigChimp)
Code: [Select]
property Parent: TParentClass read FParent write FParent;
So the problem is 1/2 solved, why Parent is not assigned in creation ?

Here all code (see attachment in earlier topic)

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, Forms, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
      Procedure testendproc;
    private
    { private declarations }
  public

    { public declarations }
  end;

  type
    TMyClass = class(TThread)
 Public
    EndProc  :  procedure of object; //<<< the Outside procedure is declared inside the Cass.
   constructor Create(CreateSuspended: Boolean; const StackSize: SizeUInt = DefaultStackSize);
    procedure Execute; override;          //<<< the Inside procedure
      end;

 type
 TParentClass = class;
    TChildClass = class(TThread)
   FParent: TParentClass;
   procedure Execute; override;          //<<< the Inside procedure
 constructor Create(CreateSuspended: Boolean; const StackSize: SizeUInt = DefaultStackSize);
  property Parent: TParentClass read FParent write FParent;
     end;

type
TParentClass= class(Tobject)
ChildClass               : TChildClass;
EndProc  :  procedure of object; //< Procedure is declared outside the thread.
end;

var
  Form1: TForm1;


implementation

{$R *.lfm}

{ TForm1 }

Procedure TForm1.testendproc;  //< the external procedure
begin
form1.label1.caption := 'External Procedure executed' ;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
testclass : TMyClass;
begin
label1.caption := 'Init of Thread...' ;
testclass :=TMyClass.Create(true);
testclass.EndProc := @testendproc;  //< the external procedure
testclass.Start ;  // start the thread

end;

procedure TForm1.Button2Click(Sender: TObject);
var
testclass : TParentClass;
begin
   label1.caption := 'Init of Thread...' ;
testclass :=TParentClass.Create;
testclass.EndProc := @testendproc;  //< the external procedure
testclass.ChildClass := TChildClass.Create(true);
testclass.ChildClass.Start ;  // start the thread
end;

Procedure    TMyClass.Execute;   //<<< the Inside procedure
begin
EndProc ;  //<<< the Outside procedure runs
end;

 constructor TMyClass.Create(CreateSuspended: Boolean;
   const StackSize: SizeUInt);
begin
  inherited Create(CreateSuspended,StackSize);
   FreeOnTerminate := True;
    end ;

 Procedure    TChildClass.Execute;   //<<< the Inside procedure
 begin
 Parent.EndProc ;  //<<< the Outside procedure do not run
 end;

  constructor TChildClass.Create(CreateSuspended: Boolean;
    const StackSize: SizeUInt);
 begin
   inherited Create(CreateSuspended,StackSize);
    FreeOnTerminate := True;
    Parent:=FParent;
   end;

 end.
« Last Edit: December 06, 2012, 09:47:43 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [1/2 SOLVED] How to talk with parent ?
« Reply #20 on: December 06, 2012, 11:47:20 pm »
Nobody find the solution ?

Alright, i gonna give you the solution  8-)

(But User137 shows me the way)

Code: [Select]
type
       TParentClass = class;
 TChildClass = class(TThread)
   FParent: TParentClass;
   procedure Execute; override;          //<<< the Inside procedure
 constructor Create(CreateSuspended: Boolean; AParent:     
 TParentClass; const StackSize: SizeUInt = DefaultStackSize);
  property Parent: TParentClass read FParent write FParent;
     end;
///////////////////////////////////////////////////
type
TParentClass= class(Tobject)
ChildClass               : TChildClass;
EndProc  :  procedure of object; //< Procedure is declared outside the thread.
constructor Create();
end;
////////////////////////////////////////////////////
Procedure    TChildClass.Execute;   //<<< the Inside procedure
 begin
 Parent.EndProc ;  //<<< the Outside procedure
 end;
/////////////////////////////////////////////////////
  constructor TChildClass.Create(CreateSuspended: Boolean;AParent: TParentClass;
    const StackSize: SizeUInt);
 begin
   inherited Create(CreateSuspended,StackSize);
      FreeOnTerminate := True;
    Parent:=AParent;
   end;
/////////////////////////////////////////////////////////
  constructor TParentClass.Create;
   begin
  inherited Create;
     ChildClass :=TChildClass.Create(true,self); //<<<<< Here with self, assign parent
    end; 

and in form :

Code: [Select]
Procedure TForm1.testendproc;  //< the external procedure
begin
form1.label1.caption := 'External Procedure executed' ;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
testclass : TParentClass;
begin
   label1.caption := 'Init of Thread...' ;
testclass :=TParentClass.Create();
testclass.EndProc := @testendproc;  //< the external procedure
testclass.ChildClass.Start ;  // start the thread
end;

Many, many thanks for your help.  :P
« Last Edit: December 07, 2012, 12:24:47 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #21 on: December 07, 2012, 01:27:29 am »
Sorry to disturb you once with that story of parents... :-[

But if somebody can show me a "Objects as properties" way to have the same result , he is welcome  ::)

Using something like :

Quote
property Engine: TEngine read FMyEngine write FMyEngine;
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [SOLVED] How to talk with parent ?
« Reply #22 on: December 07, 2012, 09:49:59 am »
Why would you use something like that?

The child class constructor is the method/procedure that is called when the object is created. Putting a parameter in there that references the parent class forces anybody using the TChildClass to specify a parent.... which seems to be exactly what your design wants.

However, you can remove that parameter from the constructor, and do something like this (not tested):
Code: [Select]
type
       TParentClass = class;
 TChildClass = class(TThread)
   FParent: TParentClass;
   procedure Execute; override;          //<<< the Inside procedure
 constructor Create(CreateSuspended: Boolean; AParent:     
 TParentClass; const StackSize: SizeUInt = DefaultStackSize);
  property Parent: TParentClass read FParent write FParent;
     end;
///////////////////////////////////////////////////
type
TParentClass= class(Tobject)
ChildClass               : TChildClass;
EndProc  :  procedure of object; //< Procedure is declared outside the thread.
constructor Create();
end;
////////////////////////////////////////////////////
Procedure    TChildClass.Execute;   //<<< the Inside procedure
 begin
 Parent.EndProc ;  //<<< the Outside procedure
 end;
/////////////////////////////////////////////////////
  constructor TChildClass.Create(CreateSuspended: Boolean;
    const StackSize: SizeUInt);
 begin
   inherited Create(CreateSuspended,StackSize);
      FreeOnTerminate := True;
    // we can't do this anymore:
    //Parent:=AParent;
    // If we did, directly accessing the variable would be
    // 1. possible - we're inside the class that has it, so the private section variables are available
    // 2. a bit faster - no need to go through the property, directly access the variable FParent
    // 3. a bit cleaner depending on what you want: you bypass the property, which can have a Set... procedure which can change other variables in your class which may be unexpected/unwanted
    // .... so:
    // FParent:=AParent;
   end;
/////////////////////////////////////////////////////////
  constructor TParentClass.Create;
   begin
  inherited Create;
    //Now we cannot set the child's parent to ourself. This is not an improvement, actually:
    //ChildClass :=TChildClass.Create(true,self); //<<<<< Here with self, assign parent
    ChildClass :=TChildClass.Create(true);
    //... and we have to do it manually...
    ChildClass.Parent:=self;
    end; 

In your example, I'd think that every child must have a parent. In other examples, that may not be so - e.g. I'm working with test cases that can have a hierarchy, so they have parents. However, at the top of the hierarchy, there is no parent.
In that case, not forcing a parent through a constructor is a good idea....
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: [SOLVED] How to talk with parent ?
« Reply #23 on: December 07, 2012, 11:25:38 am »
Code: [Select]
constructor TChildClass.Create(CreateSuspended: Boolean;
    const StackSize: SizeUInt);
 begin
   inherited Create(CreateSuspended,StackSize);
    FreeOnTerminate := True;
    Parent:=FParent;  <<<<< here assign Parent
   end;
But your Parent is a property that reads and writes to FParent, right? So what your code does is
FParent:=FParent;  // Same as FParent:=nil; because when class is created it starts as nil.

What you could do is to make a new constructor and give the parent as parameter.

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #24 on: December 07, 2012, 01:57:18 pm »
@ BigChimp & User137 : you use my initial wrong example :
i use
Code: [Select]
constructor TChildClass.Create(CreateSuspended: Boolean;AParent: TParentClass;
    const StackSize: SizeUInt);
in place of :
Code: [Select]
constructor TChildClass.Create(CreateSuspended: Boolean;
    const StackSize: SizeUInt);

Please use the one of my earlier topic :
Quote
Posted by: Fred vS
« on: December 06, 2012, 11:47:20 pm »

(i do not know how to refer to a particular line of a topic in Post reply)

I just want to know if there is a more "OOP" way to write that working code.  :-[
« Last Edit: December 07, 2012, 02:06:12 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [SOLVED] How to talk with parent ?
« Reply #25 on: December 07, 2012, 02:08:45 pm »
No, Fred, I *modified* the code posted by you in the post before mine to strip out the AParent argument.  That was what you wanted, wasn't it?

As noted in my post, the code that you posted with the AParent seems like a good fit for you need.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #26 on: December 07, 2012, 02:15:05 pm »
@ BigChimp :  :P

PS: Not so easy to become a sexy oop man (but im on the way)...
« Last Edit: December 07, 2012, 02:22:48 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [SOLVED] How to talk with parent ?
« Reply #27 on: December 07, 2012, 03:07:20 pm »
PS: Not so easy to become a sexy oop man (but im on the way)...
At least you're halfway there already (or maybe 66.66%, depending on how you count) ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #28 on: December 07, 2012, 03:12:12 pm »
@ BigChimp : give me 2.34% more and im happy  ::)
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018