Recent

Author Topic: [solve] Object inside a class  (Read 1899 times)

cappe

  • Full Member
  • ***
  • Posts: 192
[solve] Object inside a class
« on: April 28, 2020, 11:06:40 am »
With this example

  ca = class
  public
    type

    { r }

    r = object

      procedure Esegui;
    end;
  public
    c : String;
    rec :r;
  end;

procedure ca.r.Esegui;
begin
 
  How can I access here the variable c of the class ca

end;   

What I would like to know is how can I access the procedure  ca.r.Esegui to the variable c of the class ca?
Thanks
« Last Edit: April 28, 2020, 05:23:41 pm by cappe »

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Object inside a class
« Reply #1 on: April 28, 2020, 11:52:01 am »
Ciao

It depends on what you want to achieve.

Dipende da ciò che vuoi ottenere.

(Ho dedotto che tu conosca l'italiano ma potrei sbagliarmi).
Kind regards.

cappe

  • Full Member
  • ***
  • Posts: 192
Re: Object inside a class
« Reply #2 on: April 28, 2020, 11:56:43 am »
Scrivo in inglese

procedure ca.r.Esegui;
begin
 
  How can I access here the variable c of the class ca?
 
  I want to access the variable "c" (string) of the "ca" class which contains "r"

end;   

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12593
  • FPC developer.
Re: Object inside a class
« Reply #3 on: April 28, 2020, 12:00:20 pm »
Pass a reference to the class to the objects. Nested symbols (classes,objects,records) don't automatically have access to their parent reference.

Thaddy

  • Hero Member
  • *****
  • Posts: 18672
  • Jungle wars. And failing health it seems.
Re: Object inside a class
« Reply #4 on: April 28, 2020, 12:43:42 pm »
Example program:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. type
  3.   TMyClass = class
  4.   private
  5.     Type
  6.       TmyObject = object
  7.       public
  8.         procedure doIt;
  9.       end;
  10.   public
  11.     procedure DoIt;
  12.   end;
  13.  
  14.   procedure TMyClass.TMyObject.DoIt;
  15.   begin
  16.     writeln('DoIt from nested object');
  17.   end;
  18.  
  19.   procedure TMyClass.DoIt;
  20.   var
  21.     d:TMyObject;  
  22.   begin
  23.     d.DoIt;
  24.   end;
  25.    
  26. var
  27.   c:TMyClass;
  28. begin
  29.   c:=TMyClass.Create;
  30.   try
  31.     c.doit;
  32.   finally
  33.     c.free;
  34.   end;
  35. end.

But indeed, Marco's answer is correct. The example is merely to illustrate the parent class can use variables from the child object, so you can also move that variable to the object instead.
« Last Edit: April 28, 2020, 12:57:26 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

bytebites

  • Hero Member
  • *****
  • Posts: 776
Re: Object inside a class
« Reply #5 on: April 28, 2020, 04:02:14 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { ca }
  4.  
  5.   ca = class
  6.   public
  7.     type
  8.  
  9.     { r }
  10.  
  11.     r = object
  12.       parent: ca;
  13.       procedure Esegui;
  14.     end;
  15.   public
  16.     c: string;
  17.     rec: r;
  18.     constructor Create;
  19.   end;
  20.  
  21.   { ca }
  22.  
  23.   constructor ca.Create;
  24.   begin
  25.     rec.parent := self;
  26.   end;
  27.  
  28.   procedure ca.r.Esegui;
  29.   begin
  30.     //How can I access here the variable c of the class ca
  31.     writeln(parent.c);
  32.   end;
  33.  
  34. var
  35.   ci: ca;
  36. begin
  37.   ci:=ca.create;
  38.   ci.c:='Hello';
  39.   ci.rec.Esegui;
  40. end.                                  
  41.  

Thaddy

  • Hero Member
  • *****
  • Posts: 18672
  • Jungle wars. And failing health it seems.
Re: Object inside a class
« Reply #6 on: April 28, 2020, 05:10:43 pm »
That is what Marco referred.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

cappe

  • Full Member
  • ***
  • Posts: 192
Re: Object inside a class
« Reply #7 on: April 28, 2020, 05:23:09 pm »
Nice idea.

Thank you all.

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { ca }
  4.  
  5.   ca = class
  6.   public
  7.     type
  8.  
  9.     { r }
  10.  
  11.     r = object
  12.       parent: ca;
  13.       procedure Esegui;
  14.     end;
  15.   public
  16.     c: string;
  17.     rec: r;
  18.     constructor Create;
  19.   end;
  20.  
  21.   { ca }
  22.  
  23.   constructor ca.Create;
  24.   begin
  25.     rec.parent := self;
  26.   end;
  27.  
  28.   procedure ca.r.Esegui;
  29.   begin
  30.     //How can I access here the variable c of the class ca
  31.     writeln(parent.c);
  32.   end;
  33.  
  34. var
  35.   ci: ca;
  36. begin
  37.   ci:=ca.create;
  38.   ci.c:='Hello';
  39.   ci.rec.Esegui;
  40. end.                                  
  41.  

 

TinyPortal © 2005-2018