Recent

Author Topic: how to comunicate among these objects  (Read 496 times)

Paolo

  • Hero Member
  • *****
  • Posts: 675
how to comunicate among these objects
« on: January 10, 2026, 02:53:04 pm »
Hello
I have this

Code: Pascal  [Select][+][-]
  1. Type
  2. TA = class(TObject)
  3. .
  4. End;
  5.  
and a lot of children

Code: Pascal  [Select][+][-]
  1. TA1 = class(TA)
  2. TA2 = class(TA)
  3. TAi=class(TA)
  4. ..
  5.  
Then I have also another class TB that manages the above TAi.
Let say, as an example, I have something like this

Code: Pascal  [Select][+][-]
  1. Type
  2. TB = class(TObject)
  3.    X : array of TA;
  4.  ..
  5. End;
  6.  

All went fine, unless… I have the need that one of TAi, let say TAn, needs to access a field of TB !!!
I solved the problem in a very contrived way, any suggestions from the experts ?

Regards

cdbc

  • Hero Member
  • *****
  • Posts: 2616
    • http://www.cdbc.dk
Re: how to comunicate among these objects
« Reply #1 on: January 10, 2026, 03:32:20 pm »
Hi
Introduce the "Owner Mechanism":
Code: Pascal  [Select][+][-]
  1. type
  2.   { forward decl. }
  3.   TB = class;
  4.   { the 'owned' class }
  5.   TA = class(TObject)
  6.   private
  7.     fOwner: TB;
  8.   public
  9.     { in the constructor we assign 'anOwner' to 'fOwner' }
  10.     constructor Create(anOwner: TB);
  11.   ...
  12.   end;
  13.   { actual TB class }
  14.   TB = class(TObject)
  15.     X: array of TA;
  16.   ...
  17.   end;
  18.  
  19. ////////////
  20. var
  21.   Bobj: TB;
  22. ...
  23.   Bobj:= TB.Create;
  24.   SetLength(Bobj.X,5);
  25.   Bobj.X[0]:= TA.Create(Bobj); /// create with owner
  26. ...
  27.   /// later one can do:
  28.   Bobj.X[0].fOwner.DoSomething();
  29.  
A full fledged example of this can be seen in "TCollection" & "TCollectionItem"
Take a good hard long look at the source of TCollection...  ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: how to comunicate among these objects
« Reply #2 on: January 10, 2026, 04:15:16 pm »
Alternatively derive from one of the generic object lists and define the root of your class from TComponent, which is where the owner property is introduced.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Paolo

  • Hero Member
  • *****
  • Posts: 675
Re: how to comunicate among these objects
« Reply #3 on: January 11, 2026, 06:06:32 pm »
thank you very much.

At the end my code is very close to what you suggest. Essentially I introduce a "father" of TB exposing only the thing that TAn needs, something like

Code: Pascal  [Select][+][-]
  1. TBFather = class(TObject)
  2.   function DoSomething : integer;
  3. end;
  4.  
  5. TA = class(TObject)
  6.   //blah, blah..
  7. End;
  8.  
  9. { TAn }
  10.  
  11. TAn = class(TA)
  12.   Ref : TBFather;
  13.   Constructor CreateWithRef(ARef : TBFather);
  14.   function DoSomething : integer;
  15.   // other fields here
  16. end;
  17.  
  18. TB = class(TBFather)
  19.   AB : TAn;
  20.   // other fields here
  21. end;
  22.  
  23. implementation
  24.  
  25.  
  26. constructor TAn.CreateWithRef(ARef: TBFather);
  27. begin
  28.   inherited Create;
  29.   Ref:=ARef;
  30. end;
  31.  
  32. function TAn.DoSomething: integer;
  33. begin
  34.   result:=Ref.DoSomething;
  35. end;
  36.  
  37.  

jamie

  • Hero Member
  • *****
  • Posts: 7523
Re: how to comunicate among these objects
« Reply #4 on: January 12, 2026, 12:13:21 am »
Hmm, that kind of looks convoluted :(

Not really sure what you are after however, if you base them from the TPersistent class, you could employ the notifiers that live there and those
classes can notify each other when changes are taking place.

 Those would be the Observer and Observed items I am talking about.


Jamie
 
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018