Link to home
Start Free TrialLog in
Avatar of Paer Toernell
Paer ToernellFlag for Thailand

asked on

Pointers to a function in Delphi?

I have an object, that sometimes needs to make a call to a function, and that function can be one of many. So i need to be able to tell the object what function it should use "this time". How do i:

(1) Make a var that holds the adress to a function.

(2) Tell the object what adress the function now have.

Im thinking like this:

// the function
function MyValue : integer
begin
result = 10;
end;

// the objec

procedure MyObject.SetFunction (p : funcpointer);


begin
       use p, how?
end;
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paer Toernell

ASKER

Thanx, but:

(1) I have no type "tfunction". Unknown to Delphi.

(2) In form create i need to have a variable storing the function. FormCreate dont know what variant im using. It can be MyValue1 or Myvalue2 or a complete different function...
Avatar of ThievingSix
*Points to "type TFunction=function:integer;"*

You define the TFunction yourself.
(1) - it's only 15 lines of code. can't you pay more attention? I declared it for you

(2) - I don't understand your problem. declare your variable wherever you want.
Let's try this:
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
type
  TFunction = Function(Input: Integer): Integer;
 
var
  Form1 : TForm1;
  FunctionToCall : TFunction;
 
implementation
 
{$R *.dfm}
 
 
function ThisisFunctionA(Input: Integer): Integer;
begin
  Result := Input - 1;
end;
 
function ThisisFunctionB(Input: Integer): Integer;
begin
  Result := Input + 1;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  FunctionToCall := @ThisisFunctionA;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(FunctionToCall(1)));
  FunctionToCall := @ThisisFunctionB;
  ShowMessage(IntToStr(FunctionToCall(1)));
end;
 
end.

Open in new window

guess you want to use methods instead of ordinary procedures ?

button 1 changes the value with 5

button 2 changes the proc called to function A

button 3 changes the proc called to function B
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TFunction = Function(Input: Integer): Integer of object;
 
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    FunctionToCall: TFunction;
    fValue: integer;
    function ThisisFunctionA(Delta: Integer): Integer;
    function ThisisFunctionB(Delta: Integer): Integer;
  public
    property aValue: integer read fValue write fValue;
  end;
 
var
  Form1 : TForm1;
 
implementation
 
{$R *.dfm}
 
 
function TForm1.ThisisFunctionA(Delta: Integer): Integer;
begin
  Result := fValue - Delta;
end;
 
function TForm1.ThisisFunctionB(Delta: Integer): Integer;
begin
  Result := fValue + Delta;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  fValue := 100;
  FunctionToCall := ThisisFunctionA;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  FunctionToCall(5);
  ShowMessage(IntToStr(AValue));
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  FunctionToCall := ThisisFunctionA;
end;
 
procedure TForm1.Button3Click(Sender: TObject);
begin
  FunctionToCall := ThisisFunctionB;
end;
 
end.

Open in new window

Thanx all, first example is god (all are) , i only sleep a few hours so i not c...