| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 
 | unit MethodAddress1;
 
(*
 
  This sample shows how to call a funciton by name. We use MethodAddress to
  retrive the name of a function, then we call it.
 
  Since the method belongs to a class, we need to pass the class as a prameter,
  too.
 
  In gerneral, a function - declared in a class, that is defined as
     procedure TFoo.foo(i : integer);
  is called as
     procedure TFoo.foo(obj : TObject; i : integer);
 
  Normally, Delphi (as well as C++) automatically add the object parameter. Now
  that we get a pointer to a function from MethodAddress, that does not belong to
  any class, we need to pass the parameter by hand
 
  (C) Thomas Quester
      tquester@joice.net
      http://arabella.joice.net
 
*)
 
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
 
type
 
  TTest = class
  public
    Calls : integer;
  published
    Procedure Foo;
    Procedure Test;
    Procedure Bar;
    procedure TestI(i : integer);
    procedure TestS(s : string);
    function  Fak(i : integer) : integer;
  end;
 
  // For every combination of parameters, we define a prototype
 
  TFunctionCall = procedure(obj : TObject);
  TFunctionICall = procedure(obj : TObject; i : integer);
  TFunctionSCall = procedure(obj : TObject; s : string);
  TFunctionIICall = function (obj : TObject; i : integer) : integer;
 
 
  TForm1 = class(TForm)
    Label1: TLabel;
    ComboBox1: TComboBox;
    Button1: TButton;
    Label2: TLabel;
    Label3: TLabel;
    EditPar: TEdit;
    EditResult: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
      FTest:TTest;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.DFM}
 
 
procedure TForm1.Button1Click(Sender: TObject);
var p     : pointer;
    ptr   : TFunctionCall;              // Without parametes
    ptrI  : TFunctionICall;            // (i : integer);
    ptrS  : TFunctionSCall;            // (s : string);
    ptrII : TFunctionIICall;           // (i : integer) : integer
    s     : string;
 
begin
  s := combobox1.text;
  p := FTest.MethodAddress(s);            // Get the pointer
  if p <> NIL then begin
     ptr := p;                            // Assign it to every protorype
     ptri := p;
     ptrii := p;
     ptrs := p;
     s := Lowercase(s);                  // Now, we decide, what prototype to use.
 
     if  (s = 'foo') or (s = 'bar') or (s = 'test')
                        then ptr(FTest);
     if  (s = 'testi')  then ptri(FTest,strtoint(editPar.text));
     if  (s = 'tests')  then ptrs(Ftest,EditPar.text);
     if  (s = 'fak')    then editResult.text := inttostr(ptrii(ftest,strtoint(editPar.text)));
  end else
     MessageBox(0,'Function not defined','',MB_OK);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  FTest := TTest.Create;
  FTest.Calls := 0;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
begin
  FTest.Free;
end;
 
 
 
// ------------------------------------------------------------------------
// procedure TTest Foo
// Created : Quester 09.06.99 11:04:57
// ------------------------------------------------------------------------
 
 
Procedure TTest.Foo;
begin
  messagebox(0,'foo',pchar(format('%d calls',[calls])),MB_OK);
  inc(calls);
end;
 
 
// ------------------------------------------------------------------------
// procedure TTest Bar
// Created : Quester 09.06.99 11:05:08
// ------------------------------------------------------------------------
 
 
Procedure TTest.Bar;
begin
  messagebox(0,'Bar',pchar(format('%d calls',[calls])),MB_OK);
  inc(calls);
end;
 
procedure TTest.TestI(i : integer);
begin
  messagebox(0,pchar(format('i = %d',[i])),pchar(format('%d calls',[calls])),MB_OK);
  inc(calls);
end;
 
procedure TTest.TestS(s : string);
begin
  messagebox(0,pchar(format('s = %s',[s])),pchar(format('%d calls',[calls])),MB_OK);
  inc(calls);
 
end;
function  TTest.Fak(i : integer) : integer;
var j : integer;
begin
   result := 1;
   for j := 1 to i do
     result := result * j;
end;
 
 
// ------------------------------------------------------------------------
// procedure TTest Test
// Created : Quester 09.06.99 11:05:15
// ------------------------------------------------------------------------
 
 
Procedure TTest.Test;
begin
  messagebox(0,'Test ',pchar(format('%d calls',[calls])),MB_OK);
  inc(calls);  
end;
 
 
 
end. | 
Partager