• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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 ?
0
Explorer060599
Asked:
Explorer060599
1 Solution
 
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
 
MadshiCommented:
Sorry, Mike, your comment wasn't there...
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now