Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

(...as TMethodName) .. again

Posted on 2004-04-27
12
Medium Priority
?
262 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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