Solved

Array of pointers to functions

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 105
FMX StringGrid1->Canvas->FillRect Problem 3 128
Dynamically Created Query 3 50
Graphics32 under Delphi 10.1 Berlin 2 40
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 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

23 Experts available now in Live!

Get 1:1 Help Now