Recent

Author Topic: Create and us a list of objects  (Read 2131 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 602
Create and us a list of objects
« 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.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Create and us a list of objects
« Reply #1 on: July 29, 2021, 04:40:52 pm »
Did you know about TObjectList? ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hansvb

  • Hero Member
  • *****
  • Posts: 602
Re: Create and us a list of objects
« Reply #2 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.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1088
  • Professional amateur ;-P
Re: Create and us a list of objects
« Reply #3 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:
  • You can use TFPObjectList if you want a list that doesn't implement events when it's changed
  • You can use TObjectList if you want a list that does implement events when it's changed
  • You can use the Generics Collections and use it in the way the link you posted explains

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
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018