Recent

Author Topic: How to construct a list of integers with TList functionality?  (Read 7057 times)

py2pa

  • New Member
  • *
  • Posts: 11
How to construct a list of integers with TList functionality?
« on: August 17, 2014, 02:10:01 pm »
I want to construct a list of integers, but not as an simple array. I want my list to have the full functionality that TLists provide (inserting and moving elements etc). How can I do that? Because, if I declare my list as:

Code: [Select]
my_list: TList;
and then give something like this:

Code: [Select]
my_list.Add(1);
the compiler throws the error "pointer expected". Please help.

MichaelBM

  • New Member
  • *
  • Posts: 38
Re: How to construct a list of integers with TList functionality?
« Reply #1 on: August 17, 2014, 02:14:06 pm »
You need to create the list before using the method Add().

my_list.Create;
my_list.Add('1');
Development Tool: Lazarus 2.0.6 + FPC 3.0.4
Database: Firebird 2.5.7
Operating System: Windows 10 Pro 64-bit

py2pa

  • New Member
  • *
  • Posts: 11
Re: How to construct a list of integers with TList functionality?
« Reply #2 on: August 17, 2014, 02:18:07 pm »
I have created it, just ommitted the line. Also, if I pass the number as a string to Add method ('1'), I get the same error ("expected Pointer').

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How to construct a list of integers with TList functionality?
« Reply #3 on: August 17, 2014, 02:32:00 pm »
Afaik unit fgl contains a generic version of Tlist, TList<>

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to construct a list of integers with TList functionality?
« Reply #4 on: August 17, 2014, 03:16:20 pm »
the compiler throws the error "pointer expected". Please help.
TList is a list of pointer. Specialize TFPGList with integer and use that instead.
You need to create the list before using the method Add().

my_list.Create;
my_list.Add('1');
2 mistakes:
  • constructor result is assigned to the variable that will hold it, not called with the variable itself
  • with TList, it will still generate "pointer expected" error

py2pa

  • New Member
  • *
  • Posts: 11
Re: How to construct a list of integers with TList functionality?
« Reply #5 on: August 17, 2014, 04:13:52 pm »
Yes, it worked fine with TFPGList. Thank you all.

 

TinyPortal © 2005-2018