Lazarus

Programming => General => Topic started by: Dzandaa on November 28, 2021, 01:19:10 pm

Title: specialize TObjectList
Post by: Dzandaa 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?


Title: Re: specialize TObjectList
Post by: howardpc on November 28, 2021, 02:52:34 pm
TObjectList is not a generic container.
You probably want something like TFPGObjectList from the unit fgl.
Title: Re: specialize TObjectList
Post by: bytebites 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.
Title: Re: specialize TObjectList
Post by: PascalDragon 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.
Title: Re: specialize TObjectList
Post by: jamie 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
Title: Re: specialize TObjectList
Post by: PascalDragon 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.
Title: Re: specialize TObjectList
Post by: Dzandaa on November 29, 2021, 02:13:11 pm
Hi again,

Found my problem:

TCard = class(TObject) instead    TCard = class

Now it works :)

Thank you!!!
 
Title: Re: specialize TObjectList
Post by: PascalDragon 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