Recent

Author Topic: abstract class and TList  (Read 693 times)

Dzandaa

  • Hero Member
  • *****
  • Posts: 531
  • From C# to Lazarus
abstract class and TList
« on: April 07, 2026, 04:51:05 pm »
Hi everybody

I have for example

Code: Pascal  [Select][+][-]
  1. type
  2.  c1 = class
  3. begin
  4.  function f(f1: double): double;
  5. end;
  6.  
  7. Tc1List = specialize TObjectList<c1>;  
  8.  
  9. // c2
  10. type
  11. c2 = class(c1)
  12. Private
  13.  _dl: double;
  14.  
  15. Public
  16.  constructor Create(d1, d2: double);
  17.  function f(f1: double): Boolean; override;
  18.  
  19.  property dl: double read _dl;
  20. end;
  21.  
  22. // c3
  23. type
  24. c3 = class(c1)
  25. Private
  26.  _dl: double;
  27.  
  28. Public
  29.  constructor Create(d1, d2, d3: double);
  30.  function f(f1: double): Boolean; override;
  31.  
  32.  property dl: double read _dl;
  33. end;
  34.  
  35. TcList = specialize TObjectList<c1>;  
  36.  
  37. implementation  
  38.  
  39. constructor c2.Create(d1, d2: Double);
  40. begin
  41.  inherited create;
  42.  _dl := d1+d2;
  43. end;
  44.  
  45. function c2.f(f1: double): Boolean;
  46. begin
  47.  if(_dl = f1) then return true
  48.  else return false;
  49. end;
  50.  
  51. constructor c3.Create(d1, d2, d3: Double);
  52. begin
  53.  inherited create;
  54.  _dl := d1+d2+d3;
  55. end;
  56.  
  57. function c3.f(f1: double): Boolean;
  58. begin
  59.  if(_dl = f1) then return true
  60.  else return false;
  61. end;
  62.  
  63. [code=pascal]

in a created Tc1List

is it possible in a loop to add elements in a loop and how (example welcome)

Thank you.

B->
« Last Edit: April 07, 2026, 06:21:26 pm by Dzandaa »
Regards,
Dzandaa

Thaddy

  • Hero Member
  • *****
  • Posts: 18918
  • Glad to be alive.
Re: abstract class and TList
« Reply #1 on: April 07, 2026, 05:25:48 pm »
This will fail on syntax, and a lot more....:
Code: Pascal  [Select][+][-]
  1. type
  2. class c1   // unless you use macro's this is not pascal
  3. begin
  4.  function f(f1: double): double;
  5. end;
But it is possible. Add example later when I cleaned up your code.
[edit] cleaned up, do you mean this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses generics.collections;
  3. type
  4.   c1 = class abstract
  5.   Protected
  6.     _dl: double;
  7.   public
  8.     function f(f1: double): boolean;virtual;abstract;
  9.   property dl: double read _dl;
  10.   end;
  11.  
  12. c2 = class(c1)
  13. Public
  14.   constructor Create(d1, d2: double);
  15.   function f(f1: double): boolean; override;
  16. end;
  17.  
  18. Tc2List = specialize TObjectList<c1>;  
  19.  
  20. constructor c2.Create(d1, d2: Double);
  21. begin
  22.  inherited create;
  23.  _dl := d1+d2;
  24. end;
  25.  
  26. function c2.f(f1: double): Boolean;
  27. begin
  28.  if(_dl = f1) then result := true
  29.  else result := false;
  30. end;
  31. var
  32.   Objectlist:Tc2List;
  33.   i:integer;
  34. begin
  35.   ObjectList := Tc2List.Create;
  36.   for i := 0 to 99 do
  37.     Objectlist.add(c2.Create(random,random));
  38.   for i := 0 to 99 do
  39.     writeln(ObjectList[i].dl:2:2);
  40.   Objectlist.free;
  41. end.
Because that is Object Pascal and we can work from there.

Although this is working, what is the point?
There are so many mistakes in the question code.
I hope this helps you. Also with the l and 1.
« Last Edit: April 07, 2026, 06:12:27 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Dzandaa

  • Hero Member
  • *****
  • Posts: 531
  • From C# to Lazarus
Re: abstract class and TList
« Reply #2 on: April 07, 2026, 07:01:53 pm »
Hi,
@Thaddy:

Sorry for the mistakes, but I directly enter the code in the forum, not with the IDE.

I corrected it (also without testing) :)

my goal is to create a list of c1 objects that can be either c2 or c3

Is it possible?

In my real program, my list is filled with the same item address

Regards,
Dzandaa

jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: abstract class and TList
« Reply #3 on: April 07, 2026, 11:20:28 pm »
Hi,
@Thaddy:

Sorry for the mistakes, but I directly enter the code in the forum, not with the IDE.

I corrected it (also without testing) :)

my goal is to create a list of c1 objects that can be either c2 or c3

Is it possible?

In my real program, my list is filled with the same item address

Yes its possible since your list will be expecting the C1 as the base class.
add your classes as you wish but then when you get ready to access them you can use some type check information.

If C1Object is C2 Then with C2(C1Object) do
  begin
    ......
  end;

the same with C3 and so on.
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 693
Re: abstract class and TList
« Reply #4 on: April 08, 2026, 12:47:31 pm »
TObjectList ?

Thaddy

  • Hero Member
  • *****
  • Posts: 18918
  • Glad to be alive.
Re: abstract class and TList
« Reply #5 on: April 08, 2026, 03:15:33 pm »
Yes, TObjectList<T> from generics.collections because it can auto-free the objects it contains.
« Last Edit: April 08, 2026, 03:17:13 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018