Solved

Array of pointers to functions

Posted on 2006-06-18
2
260 Views
Last Modified: 2010-04-05
Let`s say I have these functions:

function A(v1, v2: Integer): boolean;
function B(v1: Integer): boolean;
function C: boolean;

I want an array like this:
Functions: Array[1..3] of pointer_to_functions;
then I want to point the array to those function and give me an example how to call the functions from the array.

Should be simple.

Thanks
0
Comment
Question by:crystyan
2 Comments
 
LVL 6

Accepted Solution

by:
Amir Azhdari earned 250 total points
ID: 16929560
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  procedure Event1(Sender:tobject);
  procedure Event2(Sender:Tobject);
  procedure Event3(Sender:Tobject);
  function A(v1, v2: Integer): boolean;
  function B(v1: Integer): boolean;
  function C: boolean;

  end;

var
  Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.Event1(Sender:Tobject);
begin
   A(10,100);
   showmessage('Event1 Activated');
end;

procedure TForm1.Event2(Sender:Tobject);
begin
  B(1000);
  showmessage('Event2 Activated');
end;

procedure TForm1.Event3(Sender:Tobject);
begin
  C;
  showmessage('Event3 Activated');
end;




function TForm1.A(v1, v2: Integer): boolean;
begin
  Result:=v1<v2;
end;


function TForm1.B(v1: Integer): boolean;
begin
  Result:=v1<100;
end;

function tform1.C: boolean;
begin
  Result:=False;
end;


var Functions: Array[1..3] of  TNotifyEvent;

procedure TForm1.Button1Click(Sender: TObject);
begin
  case strtoint(InputBox('Which event you want to point to For Button2Click Event','Which event you want to point to For Button2Click Event :(1/2/3)','1')) of
      1: button2.OnClick:=functions[1];
      2: button2.OnClick:=functions[2];
      3: button2.OnClick:=functions[3];
     else
      showmessage('Error!, out of Range(1 or 2 or 3)');
   end;


end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Functions[1]:=Event1;
  Functions[2]:=Event2;
  Functions[3]:=Event3;
end;

end.


//////////////////////////////////
DFM
//////////////////////////////////

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 80
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 257
    Top = 29
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
  end
end
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 16934308
hmm,
not the solution i had guessed . . .
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

22 Experts available now in Live!

Get 1:1 Help Now