Solved

(...as TMethodName) .. again

Posted on 2004-04-27
12
255 Views
Last Modified: 2010-04-05
Hi experts!

I want to back to my old problem.. first you need to read this:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20886881.html

Now.. I have a problem with implement function..

type
TDynamicFunction = Procedure: String of Object;

Above declaration is incorrect and Delphi don`t want to compile...

But..

type
TDynamicFunction = Function: String of Object;

is correct ! .. but when I want to "call" it by @ Method Addres I always get NIL ... Could you help me and show one example how to use "Method Adress" with functions ?

PS: sorry for my bad language..
0
Comment
Question by:Deti
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10931515
like this:

TDynamicFunction = Procedure (value: String) of Object;

TDynamicFunction = Procedure (var value: String) of Object;


Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10931548
but then you will need a varibale of type TDynamicFunction

var DynFunc: TDynamicFunction;


or in your form

TfrmMain = class(TForm)
  private
    { Private declarations }
    DynFunc: TDynamicFunction;
  public
    { Public declarations }
  end;


SHane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10931650
Oops sorry, then -  @DynFunc


Shane
0
 
LVL 2

Author Comment

by:Deti
ID: 10932060
Yes yes.. but this is not what I wanted... I want to function.. to result some value.. as I wrote:

ype
TDynamicFunction = Procedure: String of Object;

Above declaration is incorrect and Delphi don`t want to compile...

But..

type
TDynamicFunction = Function: String of Object;

.. always get NUL :(

Give me some working example... and 400 points are yours.. :)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10932248
unit Unit1;

interface

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

type

  TDynFunc = function : String of Object;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    fDynFunc: TDynFunc;
  public
    { Public declarations }
    function MyDynFunct: String;
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.MyDynFunct: String;
begin
 //
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 fDynFunc:= MyDynFunct;
end;


Shane
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10932279

You need to expose the dynamic functions in the published section for RTTI to work

type
  TDynamicFunction  =  Function: String of object;

type
  TForm1            =  class(TForm)
     procedure      FormCreate(Sender: TObject);
  private
     // Private declarations
  public
     // Public declarations
     function       CallDynamic(DynaName: String): String;
  published
     // Published declarations. NOTE: In order for RTTI to work, you must put
     // the dynamic functions in the published section
     function       Foo: String;
     function       Bar: String;
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function TForm1.Foo: String;
begin
  result:='Foo';
end;

function TForm1.Bar: String;
begin
  result:='Bar';
end;

function TForm1.CallDynamic(DynaName: String): String;
var  dynaFunc:   TDynamicFunction;
begin

  try
     // Get method address
     @dynaFunc:=MethodAddress(DynaName);
     // Check assignment
     if Assigned(@dynaFunc) then
        // Call function
        result:=dynaFunc
     else
        // Nil function
        result:='';
  except
     // Failure
     result:='';
  end;

end;

this allows for calling like

result:=Form1.CallDynamic('Foo');
result:=Form1.CallDynamic('Bar');

-------------

Regards,
Russell




0
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

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10932359
Yup! For RTTI to work

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10932361
Yup! For RTTI to work

Shane
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10935841
listening . . . all said

(sorry for the typo of the previous q (procedure -> function))

0
 
LVL 2

Author Comment

by:Deti
ID: 10937231
Yess - it works - it is okej .. points for rllibby of course.. - for the end.. could you give me some example for using function with parameter. ?

function SomeF(s: string): string;

I don`t know what is wrong:

------------------------------------------
unit Unit1;

interface

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

type
  TDynamicFunction  =  Function: String of object;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
  function CallDynamic(DynaName: String): String;
    { Public declarations }
  published

  function       Foo(s: string): String; // here you have some...
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.Foo(s: string): String;
begin
  result:=AnsiUpperCase(s);
end;

function TForm1.CallDynamic(DynaName: String): String;
var  dynaFunc:   TDynamicFunction;
begin

  try
     // Get method address
     @dynaFunc:=MethodAddress(DynaName);
     // Check assignment
     if Assigned(@dynaFunc) then
        // Call function
        result:=dynaFunc
     else
        // Nil function
        result:='';
  except
     // Failure
     result:='';
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Form1.CallDynamic('Foo(''hahaha'')')); // is it correct ?


end;

end.

Above should give me Message 'HAHAHA' ... but returns '' :(
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10937425
this should be already discussed in the previous q
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 400 total points
ID: 10938952
type
  TDynaFunc         =  Function: String of object;
  TDynaFuncParam    =  Function(Param: String): String of object;

type
  TForm1            =  class(TForm)
     Button1:       TButton;
     procedure      FormCreate(Sender: TObject);
  private
     // Private declarations
  public
     // Public declarations
     function       CallDynamicParam(DynaName: String; Param: String): String;
     function       CallDynamic(DynaName: String): String;
  published
     // Published declarations. NOTE: In order for RTTI to work, you must put
     // the dynamic functions in the published section
     function       FooParam(Param: String): String;
     function       BarParam(Param: String): String;
     function       Foo: String;
     function       Bar: String;
  end;

function TForm1.Foo: String;
begin
  result:='Foo';
end;

function TForm1.Bar: String;
begin
  result:='Bar';
end;

function TForm1.FooParam(Param: String): String;
begin
  result:=AnsiUpperCase(Param);
end;

function TForm1.BarParam(Param: String): String;
begin
  result:=AnsiLowerCase(Param);
end;

function TForm1.CallDynamicParam(DynaName: String; Param: String): String;
var  dynaFunc:   TDynaFuncParam;
begin

  // To pacify the compiler warnings
  dynaFunc:=nil;
  try
     // Get method address
     @dynaFunc:=MethodAddress(DynaName);
     // Check assignment
     if Assigned(@dynaFunc) then
        // Call function
        result:=dynaFunc(Param)
     else
        // Nil function
        result:='';
  except
     // Failure
     result:='';
  end;

end;

function TForm1.CallDynamic(DynaName: String): String;
var  dynaFunc:   TDynaFunc;
begin

  // To pacify the compiler warnings
  dynaFunc:=nil;
  try
     // Get method address
     @dynaFunc:=MethodAddress(DynaName);
     // Check assignment
     if Assigned(@dynaFunc) then
        // Call function
        result:=dynaFunc
     else
        // Nil function
        result:='';
  except
     // Failure
     result:='';
  end;

end;

--------------------


  ShowMessage(CallDynamic('Foo'));
  ShowMessage(CallDynamicParam('FooParam', 'hahaha'));

--------------------

Russell
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

18 Experts available now in Live!

Get 1:1 Help Now