Recent

Author Topic: Pattern for defining class or record inside another class - [solved]  (Read 1528 times)

jbmckim

  • Full Member
  • ***
  • Posts: 139
Are there any examples/patterns for defining a record or class with a sub class or containing a record?...asking for a friend...I wish.

Thanks.
« Last Edit: January 27, 2020, 07:37:21 pm by jbmckim »

Avinash

  • New Member
  • *
  • Posts: 25
Re: Pattern for defining class or record inside another class
« Reply #1 on: January 24, 2020, 04:27:31 am »
Record dealing is easy
With a class such a technique may be impossible, I don’t know.

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   T = record
  4.     A: Integer;
  5.     R: record
  6.       B: Integer;
  7.     end;
  8.     T: record
  9.       C: Integer;
  10.     end;
  11.     D: Integer;
  12.   end;
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. type
  4.  
  5.   M = class
  6.   end;
  7.  
  8.   L = class
  9.     A: Integer;
  10.     R: M;
  11.     T: record
  12.       C: Integer;
  13.     end;
  14.     D: Integer;
  15.   end;

jbmckim

  • Full Member
  • ***
  • Posts: 139
First apologies.  I typed this late last night and apparently deleted a few sentences, meaning to edit, before posting...Then I didn't put back the important part :-X.  This part isn't  in my title either so I definitel need to stay away from late night posting.

I'm looking for a way to use the embedded record/class as a List. I found this: https://forum.lazarus.freepascal.org/index.php/topic,30764.0.html.  It's a general direction but goes to a specific place which isn't helpful to my requirement.

So, I'd like a pattern/suggestion for creating a List of records (or a list of objects) within a class.

I've tried things like:
Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. uses
  4.   Classes, SysUtils, Math, fgl;
  5.  
  6. type
  7.   TCurvePointListRecord = Record
  8.     iNum : integer;
  9.     X : double;
  10.     Y : double;
  11.     Date_Time : TDateTime;
  12.   end;
  13.  
  14.  
  15. type
  16.  
  17.   TWvLngthCurve = Class
  18.     const
  19.       CURVE_1 : integer = 0;
  20.       CURVE_2 : integer = 1;
  21.       CURVE_3 : integer = 2;
  22.       CURVE_4 : integer = 3;
  23.  
  24.     public
  25.  
  26.       WvToTrack : integer;
  27.       WvMin : integer;
  28.       WvMax : integer;
  29.       WvMinAlarm : integer;
  30.       WvMaxAlarm : integer;
  31.       WvNumber : integer;
  32.       WvActive : boolean;
  33.       ThisYHigh : boolean;
  34.       ThisYLow : boolean;
  35.       Num : integer;
  36.  
  37.       PCurvePointListRecord : ^TCurvePointListRecord;
  38.       TCurvePointList : specialize TFPGObjectList<PCurvePointListRecord>;
  39.  
  40.     public
  41.       constructor Init(CurveNumber : integer);
  42.       procedure InitWvValues(CurveNumber : integer);
  43.       procedure LoadVarsFromDB(CurveNumber : integer ; DBString : string);
  44.       function ConvertStringToBool(inStr : string) : boolean;
  45.       procedure FindNewY(var NewY : currency ; AvgArray : array of double);
  46.       procedure CleanUpOnClose();
  47.       procedure ListClear();
  48.  
  49.  
  50.     end;
  51.  
  52. implementation
  53. uses
  54.   WavelengthAvgRuntime;
  55.  
  56. var
  57.   CurvePointList : TCurvePointList;
  58.  
  59.  
  60. constructor TWvLngthCurve.Init(CurveNumber : integer);
  61. begin
  62.    InitWvValues(CurveNumber);
  63. end;                              

This fails to compile at line 38 (as above) with: wvlngthcurveunit.pas(42,51) Error: Identifier not found "PCurvePointListRecord". 

I've moved this line around some and it always seem to have some sort of compile issue with it.

(Note that I've updated the subject line here to make it consistent with the body.)

Thanks.

 

howardpc

  • Hero Member
  • *****
  • Posts: 3548
The following compiles OK (the specialization requires a class, not a record).
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   fgl;
  9.  
  10. type
  11.   TCurvePointListClass = class
  12.     iNum : integer;
  13.     X : double;
  14.     Y : double;
  15.     Date_Time : TDateTime;
  16.   end;
  17.  
  18.   TCurvePointList = specialize TFPGObjectList<TCurvePointListClass>;
  19.  
  20.   TWvLngthCurve = Class
  21.     const
  22.       CURVE_1 : integer = 0;
  23.       CURVE_2 : integer = 1;
  24.       CURVE_3 : integer = 2;
  25.       CURVE_4 : integer = 3;
  26.     public
  27.       WvToTrack : integer;
  28.       WvMin : integer;
  29.       WvMax : integer;
  30.       WvMinAlarm : integer;
  31.       WvMaxAlarm : integer;
  32.       WvNumber : integer;
  33.       WvActive : boolean;
  34.       ThisYHigh : boolean;
  35.       ThisYLow : boolean;
  36.       Num : integer;
  37.     public
  38.       constructor Init(CurveNumber : integer);
  39.       procedure InitWvValues(CurveNumber : integer);
  40.       procedure LoadVarsFromDB(CurveNumber : integer ; DBString : string);
  41.       function ConvertStringToBool(inStr : string) : boolean;
  42.       procedure FindNewY(var NewY : currency ; AvgArray : array of double);
  43.       procedure CleanUpOnClose();
  44.       procedure ListClear();
  45.     end;
  46.  
  47. implementation
  48.  
  49. var
  50.   CurvePointList : TCurvePointList;
  51.  
  52. constructor TWvLngthCurve.Init(CurveNumber : integer);
  53. begin
  54.    InitWvValues(CurveNumber);
  55. end;
  56.  
  57. procedure TWvLngthCurve.InitWvValues(CurveNumber: integer);
  58. begin
  59.  
  60. end;
  61.  
  62. procedure TWvLngthCurve.LoadVarsFromDB(CurveNumber: integer; DBString: string);
  63. begin
  64.  
  65. end;
  66.  
  67. function TWvLngthCurve.ConvertStringToBool(inStr: string): boolean;
  68. begin
  69.  
  70. end;
  71.  
  72. procedure TWvLngthCurve.FindNewY(var NewY: currency; AvgArray: array of double);
  73. begin
  74.  
  75. end;
  76.  
  77. procedure TWvLngthCurve.CleanUpOnClose();
  78. begin
  79.  
  80. end;
  81.  
  82. procedure TWvLngthCurve.ListClear();
  83. begin
  84.  
  85. end;
  86.  
  87. end.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8785
  • FPC developer.
Have a look at something like this attempt at a generic substitute for tstringlist with an extra level of indirection:

http://www.stack.nl/~marcov/genvarmap.zip
« Last Edit: January 24, 2020, 10:55:33 pm by marcov »

jbmckim

  • Full Member
  • ***
  • Posts: 139
marcov,

Can you provide a hyperlinked url or other address?  Neither my FF or Chrome browsers can find anything associated with that url.

Thanks

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8785
  • FPC developer.
typo fixed

jbmckim

  • Full Member
  • ***
  • Posts: 139
   
howardpc:

I get a peculiar compile error when I embed this in the real unit.  (Note the add of "CurvePointList.Add(TCurvePointListClass);  at line 75.)  This produces the error @75 of:

Code: Pascal  [Select][+][-]
  1. wvlngthcurveunit.pas(346,42) Error: Incompatible type for arg no. 1: Got "Class Of TCurvePointListClass", expected "TCurvePointListClass"

Is it looking for a variable rather than a class declaration?  That would seem inconsistent with my code elsewhere.


Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.      
  3.     {$mode objfpc}{$H+}
  4.      
  5.     interface
  6.      
  7.     uses
  8.       fgl;
  9.      
  10.     type
  11.       TCurvePointListClass = class
  12.         iNum : integer;
  13.         X : double;
  14.         Y : double;
  15.         Date_Time : TDateTime;
  16.       end;
  17.      
  18.       TCurvePointList = specialize TFPGObjectList<TCurvePointListClass>;
  19.      
  20.       TWvLngthCurve = Class
  21.         const
  22.           CURVE_1 : integer = 0;
  23.           CURVE_2 : integer = 1;
  24.           CURVE_3 : integer = 2;
  25.           CURVE_4 : integer = 3;
  26.         public
  27.           WvToTrack : integer;
  28.           WvMin : integer;
  29.           WvMax : integer;
  30.           WvMinAlarm : integer;
  31.           WvMaxAlarm : integer;
  32.           WvNumber : integer;
  33.           WvActive : boolean;
  34.           ThisYHigh : boolean;
  35.           ThisYLow : boolean;
  36.           Num : integer;
  37.         public
  38.           constructor Init(CurveNumber : integer);
  39.           procedure InitWvValues(CurveNumber : integer);
  40.           procedure LoadVarsFromDB(CurveNumber : integer ; DBString : string);
  41.           function ConvertStringToBool(inStr : string) : boolean;
  42.           procedure FindNewY(var NewY : currency ; AvgArray : array of double);
  43.           procedure CleanUpOnClose();
  44.           procedure ListClear();
  45.         end;
  46.      
  47.     implementation
  48.      
  49.     var
  50.       CurvePointList : TCurvePointList;
  51.      
  52.     constructor TWvLngthCurve.Init(CurveNumber : integer);
  53.     begin
  54.        InitWvValues(CurveNumber);
  55.     end;
  56.      
  57.     procedure TWvLngthCurve.InitWvValues(CurveNumber: integer);
  58.     begin
  59.      
  60.     end;
  61.      
  62.     procedure TWvLngthCurve.LoadVarsFromDB(CurveNumber: integer; DBString: string);
  63.     begin
  64.      
  65.     end;
  66.      
  67.     function TWvLngthCurve.ConvertStringToBool(inStr: string): boolean;
  68.     begin
  69.      
  70.     end;
  71.      
  72.     procedure TWvLngthCurve.FindNewY(var NewY: currency; AvgArray: array of double);
  73.     begin
  74.  
  75.        CurvePointList.Add(TCurvePointListClass);    
  76.      
  77.     end;
  78.      
  79.     procedure TWvLngthCurve.CleanUpOnClose();
  80.     begin
  81.      
  82.     end;
  83.      
  84.     procedure TWvLngthCurve.ListClear();
  85.     begin
  86.      
  87.     end;
  88.      
  89.     end.

jbmckim

  • Full Member
  • ***
  • Posts: 139
Okay, answered my own question.  I need a local var to load and then add that to the list.

Code: Pascal  [Select][+][-]
  1.     unit Unit2;
  2.          
  3.         {$mode objfpc}{$H+}
  4.          
  5.         interface
  6.          
  7.         uses
  8.           fgl;
  9.          
  10.         type
  11.           TCurvePointListClass = class
  12.             iNum : integer;
  13.             X : double;
  14.             Y : double;
  15.             Date_Time : TDateTime;
  16.           end;
  17.          
  18.           TCurvePointList = specialize TFPGObjectList<TCurvePointListClass>;
  19.          
  20.           TWvLngthCurve = Class
  21.             const
  22.               CURVE_1 : integer = 0;
  23.               CURVE_2 : integer = 1;
  24.               CURVE_3 : integer = 2;
  25.               CURVE_4 : integer = 3;
  26.             public
  27.               WvToTrack : integer;
  28.               WvMin : integer;
  29.               WvMax : integer;
  30.               WvMinAlarm : integer;
  31.               WvMaxAlarm : integer;
  32.               WvNumber : integer;
  33.               WvActive : boolean;
  34.               ThisYHigh : boolean;
  35.               ThisYLow : boolean;
  36.               Num : integer;
  37.             public
  38.               constructor Init(CurveNumber : integer);
  39.               procedure InitWvValues(CurveNumber : integer);
  40.               procedure LoadVarsFromDB(CurveNumber : integer ; DBString : string);
  41.               function ConvertStringToBool(inStr : string) : boolean;
  42.               procedure FindNewY(var NewY : currency ; AvgArray : array of double);
  43.               procedure CleanUpOnClose();
  44.               procedure ListClear();
  45.             end;
  46.          
  47.         implementation
  48.          
  49.         var
  50.           CurvePointList : TCurvePointList;
  51.          
  52.         constructor TWvLngthCurve.Init(CurveNumber : integer);
  53.         begin
  54.            InitWvValues(CurveNumber);
  55.         end;
  56.          
  57.         procedure TWvLngthCurve.InitWvValues(CurveNumber: integer);
  58.         begin
  59.          
  60.         end;
  61.          
  62.         procedure TWvLngthCurve.LoadVarsFromDB(CurveNumber: integer; DBString: string);
  63.         begin
  64.          
  65.         end;
  66.          
  67.         function TWvLngthCurve.ConvertStringToBool(inStr: string): boolean;
  68.         begin
  69.          
  70.         end;
  71.          
  72.         procedure TWvLngthCurve.FindNewY(var NewY: currency; AvgArray: array of double);
  73.         var
  74.            LocalCPList : TCurvePointListClass;
  75.  
  76.         begin
  77.  
  78.             LocalCPList.X := SomeNumber;
  79.             LocalCPList.Y := SomeOtherNumber;
  80.  
  81.             //LocalCPList.Y := etc.
  82.    
  83.            CurvePointList.Add(LocalCPList);    
  84.          
  85.         end;
  86.  
  87.  
  88.          
  89.         procedure TWvLngthCurve.CleanUpOnClose();
  90.         begin
  91.          
  92.         end;
  93.          
  94.         procedure TWvLngthCurve.ListClear();
  95.         begin
  96.          
  97.         end;
  98.          
  99.         end.

And I will add...Duh.

jbmckim

  • Full Member
  • ***
  • Posts: 139
I edited some of the above embedded code as a pattern for using a list of objects.  marcov - it looks like you're building a more generic solution?  This would be very helpful.  I'm not exploring your draft at this time only because I have a project I must continue with.  In any case, I think it's worth noting that there are very generic lists, with both hashed and sequential access available in .Net.  Any movement toward generic list build is definitely worthwhile IMO.

This code has had some extraneous declarations removed and I have added clear and free statements.  I did not compile and this will not run.  It is only intended as an example in between pseudo code and real code.

Code: Pascal  [Select][+][-]
  1.         unit Unit2;
  2.              
  3.             {$mode objfpc}{$H+}
  4.              
  5.             interface
  6.              
  7.             uses
  8.               fgl;
  9.              
  10.             type
  11.               TCurvePointListClass = class
  12.                 iNum : integer;
  13.                 X : double;
  14.                 Y : double;
  15.                 Date_Time : TDateTime;
  16.               end;
  17.              
  18.               TCurvePointList = specialize TFPGObjectList<TCurvePointListClass>;
  19.              
  20.               TWvLngthCurve = Class
  21.                 const
  22.                   CURVE_1 : integer = 0;
  23.        
  24.                 public
  25.                   WvToTrack : integer;
  26.                   WvMin : integer;
  27.                   WvMax : integer;
  28.                   Num : integer;
  29.                 public
  30.                   constructor Init(CurveNumber : integer);
  31.                   procedure InitValues(CurveNumber : integer);
  32.                   procedure LoadVarsFromDB(CurveNumber : integer ; DBString : string);
  33.                   function ConvertStringToBool(inStr : string) : boolean;
  34.                   procedure FindNewY(var NewY : currency ; AvgArray : array of double);
  35.                   procedure CleanUpOnClose();
  36.                   procedure ListClear();
  37.                 end;
  38.              
  39.             implementation
  40.          
  41.             constructor TWvLngthCurve.Init(CurveNumber : integer);
  42.             begin
  43.                InitValues(CurveNumber);
  44.             end;
  45.              
  46.             procedure TWvLngthCurve.InitValues(CurveNumber: integer);
  47.             begin
  48.              
  49.             end;
  50.              
  51.             procedure TWvLngthCurve.LoadVarsFromDB(CurveNumber: integer; DBString: string);
  52.             begin
  53.              
  54.             end;
  55.              
  56.             function TWvLngthCurve.ConvertStringToBool(inStr: string): boolean;
  57.             begin
  58.              
  59.             end;
  60.              
  61.             procedure TWvLngthCurve.FindNewY(var NewY: currency; AvgArray: array of double);
  62.             var
  63.                LocalCPList : TCurvePointListClass;
  64.      
  65.             begin
  66.  
  67.                 LocalCPList := TCurvePointListClass.Create;
  68.  
  69.                 LocalCPList.X := SomeNumber;
  70.                 LocalCPList.Y := SomeOtherNumber;
  71.      
  72.                CurvePointList.Add(LocalCPList);    
  73.              
  74.             end;
  75.      
  76.      
  77.              
  78.             procedure TWvLngthCurve.CleanUpOnClose();
  79.             begin
  80.  
  81.                 CurvePointList.Clear();
  82.                 CurvePointList.Free();
  83.              
  84.             end;
  85.              
  86.             procedure TWvLngthCurve.ListClear();
  87.             begin
  88.              
  89.              
  90.             end;
  91.              
  92.             end.

Please feel free to edit for reasons of correctness and/or clarity.

 

TinyPortal © 2005-2018