Solved

Pointers to a function in Delphi?

Posted on 2008-06-10
7
182 Views
Last Modified: 2010-05-18
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;
0
Comment
Question by:Sunsales
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 28

Accepted Solution

by:
ciuly earned 500 total points
ID: 21753913

type

  TFunction=function:integer;
 

var f:TFunction;
 

function MyValue : integer;

begin

  result := 10;

end;
 

procedure TForm1.FormCreate(Sender: TObject);

begin

  f:=@myvalue;

  showmessage(inttostr(f));

end;

Open in new window

0
 

Author Comment

by:Sunsales
ID: 21754325
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...
0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 21754418
*Points to "type TFunction=function:integer;"*

You define the TFunction yourself.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 28

Expert Comment

by:ciuly
ID: 21754425
(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.
0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 21755019
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

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 21756659
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

0
 

Author Comment

by:Sunsales
ID: 21760362
Thanx all, first example is god (all are) , i only sleep a few hours so i not c...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Splash screen too shortlived 11 113
code issue 8 85
How to debug For loops? 3 34
Error E2158 compiling with Delphi XE10 Seattle 2 49
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now