Recent

Author Topic: specialize TObjectList  (Read 5953 times)

Dzandaa

  • New Member
  • *
  • Posts: 41
specialize TObjectList
« on: November 28, 2021, 01:19:10 pm »
 Hi everybody,

 Working with Lazarus Pascal 2.012 FPC 2.3.0 on Windows and Linux
I'm new to Lazarus for a few months so be cool :)

 I have 3 Units to create a Card Game:
 
 A Form Unit for the main program
 A Card Unit to store the Card Parameters (Class TCard).
 A Deskcreator Unit to create a desk card (Class TDeskCreator).
 Containing :

Code: Pascal  [Select][+][-]
  1.  public
  2.   CardsList: specialize TObjectList<Tcard>; // To store the list of Cards
  3.   function CreateCards():specialize TObjectList<Tcard>; to create the Desk Card
  4.  
  5.  function TDeskCreator.CreateCards():specialize TObjectList<Tcard>;
  6.  var
  7.   ...
  8.  begin
  9.   CardsList := specialize TObjectList<Tcard>.Create;
  10.   ...
  11.   CardsList.Add(NewCard);  
  12.   ...
  13.   // Here CardList.Count = 52, it's O.K.
  14.   // All Cards in the List are valid
  15.   exit(CardsList);
  16. // also try Result := CardsList;
  17.  end
  18.  

A Form Unit for the main program (Class TCardMagicForm).
 Containing:

Code: Pascal  [Select][+][-]
  1. private
  2.  MyDesk: TDeskCreator;  
  3.  Cards: specialize TObjectList<Tcard>;  
  4.    
  5. procedure TCardMagicForm.InitDesk();
  6. begin
  7.   MyDesk := TDeskCreator.Create;
  8.  
  9.   Cards := MyDesk.CreateCards();
  10.  
  11. // Here Cards.Count = 0, Not O.K.
  12. end;  
  13.  

My problem:

I don't know how to return my TObjectList from my Deskcreator to my main program...

The Count in the list is always 0 in the main form.

If I remove the Deskcreator Unit and put all except the TCard Unit in the main Form, it is working !!!

Any idea?



howardpc

  • Hero Member
  • *****
  • Posts: 3972
Re: specialize TObjectList
« Reply #1 on: November 28, 2021, 02:52:34 pm »
TObjectList is not a generic container.
You probably want something like TFPGObjectList from the unit fgl.

bytebites

  • Sr. Member
  • ****
  • Posts: 473
Re: specialize TObjectList
« Reply #2 on: November 28, 2021, 06:27:00 pm »
TObjectList is not a generic container.
You probably want something like TFPGObjectList from the unit fgl.

But TObjectList from Generics.Collections is generic.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4005
  • Compiler Developer
Re: specialize TObjectList
« Reply #3 on: November 29, 2021, 01:16:58 pm »
If I remove the Deskcreator Unit and put all except the TCard Unit in the main Form, it is working !!!

Would you please provide a small, but complete example, so that we can try your code ourselves? Working with code snippets more often than not does not contain the part that is really relevant.

jamie

  • Hero Member
  • *****
  • Posts: 4582
Re: specialize TObjectList
« Reply #4 on: November 29, 2021, 01:47:13 pm »
I don't know if it makes any difference but what I noticed is the collections units is all in Delphi Mode using "Abtract" and code here is using fpc mode specialized etc..

  I don't know if that makes any difference between units and using different modes
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 4005
  • Compiler Developer
Re: specialize TObjectList
« Reply #5 on: November 29, 2021, 02:03:06 pm »
I don't know if that makes any difference between units and using different modes

No, it does not.

Dzandaa

  • New Member
  • *
  • Posts: 41
Re: specialize TObjectList
« Reply #6 on: November 29, 2021, 02:13:11 pm »
Hi again,

Found my problem:

TCard = class(TObject) instead    TCard = class

Now it works :)

Thank you!!!
 

PascalDragon

  • Hero Member
  • *****
  • Posts: 4005
  • Compiler Developer
Re: specialize TObjectList
« Reply #7 on: November 30, 2021, 01:36:31 pm »
Found my problem:

TCard = class(TObject) instead    TCard = class

Now it works :)

That should not make a difference :o You sure that you only changed that?

 

TinyPortal © 2005-2018