Recent

Author Topic: [Sovled] What I am I doing wrong  (Read 634 times)

DreamVB

  • Full Member
  • ***
  • Posts: 100
    • Memo Pad
[Sovled] What I am I doing wrong
« on: August 24, 2023, 07:25:10 pm »
Hi, I trying to make a TInteger list like a string list but for ints I got adding working and fetching items but stuck on the sort it won't compile and gives an error any idea what I am doing wrong I attached the project and class file.

Thanks.
« Last Edit: August 24, 2023, 08:31:21 pm by DreamVB »
Dream Believe Achieve

Lulu

  • Sr. Member
  • ****
  • Posts: 405
Re: What I am I doing wrong
« Reply #1 on: August 24, 2023, 08:28:28 pm »
Hi, a simpler way is to use generic from unit fgl.
The content of your unit listint will be:
Code: Pascal  [Select][+][-]
  1. unit listint;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, fgl;
  9.  
  10. type
  11.  
  12.   { TListInt }
  13.  
  14.   TListInt = class(specialize TFPGList<Integer>)
  15.   public
  16.     procedure Sort;
  17.   end;
  18.  
  19. function SortProc(const Item1, Item2: Integer): integer;
  20.  
  21. implementation
  22.  
  23. function SortProc(const Item1, Item2: Integer): integer;
  24. begin
  25.   if Item1 < Item2 then Result := -1
  26.   else if Item1 > Item2 then Result := 1
  27.   else
  28.     Result := 0;
  29. end;
  30.  
  31. { TListInt }
  32.  
  33. procedure TListInt.Sort;
  34. begin
  35.   inherited Sort(@SortProc);
  36. end;
  37. end.
In attachment, your project with some correction in Unit1.

You can learn about generics HERE and in this forum.
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

DreamVB

  • Full Member
  • ***
  • Posts: 100
    • Memo Pad
[Sovled] Re: What I am I doing wrong
« Reply #2 on: August 24, 2023, 08:31:03 pm »
Thanks Lulu that sorted it ;) I also noticed I missed to put the @ before the procedure and it works thanks for your version to.
Dream Believe Achieve

Lulu

  • Sr. Member
  • ****
  • Posts: 405
Re: [Sovled] What I am I doing wrong
« Reply #3 on: August 24, 2023, 08:37:51 pm »
I also had a compilation error because I've forgotten 'const' in function SortProc(const...) ! :)
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

 

TinyPortal © 2005-2018