Solved

Error: Incompatible types: regular procedure and method pointer

Posted on 2001-06-29
3
3,201 Views
Last Modified: 2012-06-21
I am getting an error (Incompatible types: regular procedure and method pointer) when using procedural types in the following maner:
TMyClass = class
  private
   function compare(a,b: integer): byte;
   procedure someProcedure;
end; {TMyClass}

procedure TMyClass.someProcedure;
begin
  mySort(compare);
end;

I have a unit called sort...

unit sort;
type
  compFunc = function (a,b: integer) : byte;

procedure mysort(compare: compFunc);
begin
  ...
end;

I can't figure out why I am getting this error message.
Thanks in advance
jeco
0
Comment
Question by:jeco
3 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6238318
Hi jeco,

it declared as function (a,b: Integer): byte  "of object".

you should declare
type
  compFunc = function (a,b: integer) : byte; of object;
and assign it not for standalone function, but for object method.

another way to pass address of function as parameter

type
  TMyClass = ...
  ...
   function compare(Proc: CompFunction);
  end;


function TMyClass.Compare(Proc: CompFunction);
begin
   if Proc(A, B) = 0 ...
....

-----
Igor.
0
 
LVL 3

Accepted Solution

by:
alzv earned 100 total points
ID: 6238325
You receive such message because you are trying to pass a method pointer as a parameter. But your procedure can only retrieve an entry point of regular function not a class method. If you want to pass a method into your procedure you need to modify CompFunc type declaration like this:

type
  CompFunc = function (a,b: integer) : byte of object;

Or if you want to pass both types of procedures (regular and methods) you need to overload your MySort procedure like this:

type
  CompFunc = function (a,b: integer) : byte;
  CompFuncMethod = function (a,b: integer) : byte of object;

procedure MySort(Compare: CompFunc); overload;
procedure MySort(Compare: CompFuncMethod); overload;

Best regards,
Alexey Zverev.
0
 

Author Comment

by:jeco
ID: 6239262
Thanks igor and Alexey for your answers. Both answers worked but since I ended up using overload procedures I will give the points to Alex
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question