Recent

Author Topic: TStringList.CustomSort Problem  (Read 5517 times)

npage

  • Newbie
  • Posts: 2
TStringList.CustomSort Problem
« on: January 04, 2013, 05:32:58 pm »
I can't figure out why, but when I try using the CustomSort procedure of any TStringList, I get this error during compile:
Error: Wrong number of parameters specified for call to "MySort"

Lazarus version 1.1
FPC version 2.6.1
SVN version 39572

Here some code:

function MySort(List: TStringList; Index1: integer; Index2: Integer): Integer;
begin
  <my code here>
end;

called in my code with:

MyStringList.CustomSort(MySort);

What am I doing wrong?
I did see a similar post from last year thinking it had something to do with the Gernerics support, but there weren't any replies to that.

Thanks
Chris

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TStringList.CustomSort Problem
« Reply #1 on: January 04, 2013, 05:57:11 pm »
Code: [Select]
MyStringList.CustomSort(@MySort);

npage

  • Newbie
  • Posts: 2
Re: TStringList.CustomSort Problem
« Reply #2 on: January 04, 2013, 06:22:22 pm »
Yup that did the trick.

I don't remember having to do that in Delphi.  Is it different, or is my memory worse than I thought?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TStringList.CustomSort Problem
« Reply #3 on: January 04, 2013, 06:29:13 pm »
It is different in Lazarus if you are using the stricter {$mode objfpc}
To use the looser Delphi syntax specify {$mode delphi}

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 390
Re: TStringList.CustomSort Problem
« Reply #4 on: March 07, 2018, 07:37:14 pm »
The same problem.

As it is, default:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}  
  2.  

Code: Pascal  [Select][+][-]
  1. function TForm1.Listsort(List:TStringList; index1,index2:integer):integer;
  2. begin
  3.     result:=0;
  4. end;    
  5.  

If I put @ infront like

Code: Pascal  [Select][+][-]
  1. sl.customsort(@Listsort);
  2.  

-->

Quote
main.pas(860,26) Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(TStringList;LongInt;LongInt):LongInt of object;Register>", expected "<procedure variable type of function(TStringList;LongInt;LongInt):LongInt;Register>"

How to fix that ?
« Last Edit: March 07, 2018, 07:43:30 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TStringList.CustomSort Problem
« Reply #5 on: March 07, 2018, 07:53:49 pm »
Declare ListSort() as an independent function in your interface, not as a method of TForm1.

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 390
Re: TStringList.CustomSort Problem
« Reply #6 on: March 07, 2018, 09:06:59 pm »
Thanks.
That solves it.
lazarus 3.2-fpc-3.2.2-win32/win64

 

TinyPortal © 2005-2018