Solved

(...as TMethodName) .. again

Posted on 2004-04-27
12
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
drawing animated level bar based on numbers 3 112
Tembedded WB animatid gifs not animated on some pcs 2 86
Base1 Encode/Decode 3 86
Dev express lookupcombo 3 46
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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