Solved

Array of pointers to functions

Posted on 2006-06-18
2
267 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

11 Experts available now in Live!

Get 1:1 Help Now