Solved

(...as TMethodName) .. again

Posted on 2004-04-27
12
256 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
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.

 
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

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

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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

20 Experts available now in Live!

Get 1:1 Help Now