procedure parameter

can i pass the procedure as parameter into procedure ? mean that l'm passing the pointer of procedure or function into one generic procedure that responsed to execute the parameter as procedure.
how can i do that ?
Explorer060599Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
Generally, you can of course use a procedure as a parameter into another procedure. But keep in mind that Delphi is quite rigid in type checking and using a normal pointer as generic function pointer and casting it to one of different types is not very Delphi like and prevents the compiler from type checking.

A typical case for a procedure parameter is procedure GetColorValues(Proc: TGetStrProc); in Graphics.pas. TGetStrProc is declared as:

type TGetStrProc = procedure(const S: string) of object;

which means it is a procedural parameter which must be declared in an object (therefor the "of object" addition). GetColorValues then uses the Proc parameter to call it with every color value it has registered.

Ciao, Mike
0
MadshiCommented:
You must know the parameters, then you can do something like this:

type
  TSpecialFunction = function (str: string) : boolean;

function ExecuteFunction(func: TSpecialFunction; str: string) : boolean;
begin
  result := func(str);
end;

function SpecialFunction1(str: string) : boolean;
begin
  result := str = '1';
end;

function SpecialFunction2(str: string) : boolean;
begin
  result := str = '2';
end;

Now you can do something like this:

ExecuteFunction(SpecialFunction1, '1');
ExecuteFunction(SpecialFunction2, 'test');

Regards, Madshi.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MadshiCommented:
Sorry, Mike, your comment wasn't there...
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

MadshiCommented:
Mike is right.

Some more infos:

You can get a pointer to a function by doing this "@SpecialFunction1". E.g. you could ask in ExecuteFunction something like this:

  if @func = nil then
    raise Exception.Create('Invalid parameter!!');

And if you have a simple pointer, you can typecast it into a function type like this:

  result := TSpecialFunction(aPointerVariable)('test');

But as Mike pointed out, that's no good programming practise, because Delphi can't check the parameters this way.
0
edeyCommented:
You need to define the procedure type (like TNotifyEvent), then you have a parameter of that type. ex:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Spin, ExtCtrls;

type
  TForm1 = class(TForm)
    SpinEdit1: TSpinEdit;
    RadioGroup1: TRadioGroup;
    Label1: TLabel;
    procedure SpinEdit1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  TVarX = procedure (var x : extended);
var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure DoubleX(var x : extended);
begin
     x := x*2;
end;

procedure TripleX(var x : extended);
begin
     x := x*3;
end;

procedure DrawLabel(lbl : TLabel;val : extended;proc : TVarX);
begin
     proc(val);
     lbl.caption := floatToStr(val);
end;

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
     if radioGroup1.itemIndex = 0 then
        drawLabel(label1,spinEdit1.value,DoubleX)
     else
         drawLabel(label1,spinEdit1.value,TripleX);
end;

end.



GL
Mike
0
Explorer060599Author Commented:
everybody is right...but i've to only give one person points..sorry the rest.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.