Lazarus

Programming => General => Topic started by: Hansvb on July 29, 2021, 04:24:28 pm

Title: Create and us a list of objects
Post by: Hansvb on July 29, 2021, 04:24:28 pm
Hi

I'm trying to convert a program I made with c# to Lazarus. How can I create the following in Pascal?
In C#, I have a class A and a class B that keeps a list of all class A objects. So in class B I can do something like items.add(object of class a) where items is of type class A. Then I can walk through the item list and retrieve data from the individual A-type objects.

How can I make that best in Pascal?  Do I have to make 2 classes or is a record type and a class better. I found something from Generics that I think is a bit like what I'm looking for.

I need some advice in the right direction.
Title: Re: Create and us a list of objects
Post by: lucamar on July 29, 2021, 04:40:52 pm
Did you know about TObjectList (https://www.freepascal.org/docs-html/fcl/contnrs/tobjectlist.html)? ;)
Title: Re: Create and us a list of objects
Post by: Hansvb on July 29, 2021, 04:50:31 pm
I think i found what I need. I will look at this:
https://forum.lazarus.freepascal.org/index.php?topic=43625.0

TObjectlist is also good i think.
Title: Re: Create and us a list of objects
Post by: Gustavo 'Gus' Carreno on July 29, 2021, 08:28:04 pm
Hey Hansvb,

I need some advice in the right direction.

At the moment you have 3 options:

Your choice of solutions depending on the overhead you can deal with.

For very simple things I use TFPObjectList, but if you're coming from a C# background, I understand you'll be more comfortable with the Generics Collections :)

I haven't dived into the Generics flow yet, but I'm an old grumpy programmer and I need a bit more time for the new things to sink in :)

Cheers,
Gus
TinyPortal © 2005-2018