Fórum Executando um metodo(Function) pelo nome #385271
31/08/2010
0
Preciso da ajuda de vocês....
Alguém já executou um método(Function) pelo nome?
O exemplo abaixo consegui executar, porém chamando uma procedure, gostaria de fazer o mesmo, mas chamando uma function com parâmetros.
Segue o exemplo que fiz:
procedure ExecMethod(Func: string);var Routine: TMethod; Exec: TExec;begin Routine.Data := Application; Routine.Code := MethodAddress(Func); if Assigned(Routine.Code) then begin Exec := TExec(Routine); Exec; end;end;
ExecMethod('MostraConteudo');
Muito Obrigada e aguardo o Retorno de vocês.
--
Com Estima,
Lorena Coelho
Lorena
Curtir tópico
+ 0Post mais votado
31/08/2010
published
function MostraConteudo(str: String):String;
...
function TForm6.MostraConteudo(str: String):String;
begin
result:=str; // so a titulo de exemplo
end;
Altere
type
TExec = function(str:String):string of object; // Altere o Tipo
Agora faça :
function ExecMethod(Func: string;str:String):String;
var
Routine: TMethod;
Exec: TExec;
begin
Routine.Code := form6.MethodAddress(Func);
if Assigned(Routine.Code) then
begin
Exec := TExec(Routine);
result:=Exec(str); // Aqui passa os Parametros
end;
end;
Para chamar faça
procedure TForm6.Button1Click(Sender: TObject);
var
str:String;
begin
Str:='Qualquer Coisa'
showmessage(ExecMethod('MostraConteudo',str));
end;
Marco Salles
Gostei + 1
Mais Posts
31/08/2010
Leonardo Xavier
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, JvExButtons, JvButtons, DB, DBClient,
wwclient, JvDataSource, Grids, DBGrids, JvgDBGrid, JvExControls,
JvXPCore, JvXPButtons;
type
TForm1 = class(TForm)
Button1: TJvHTButton;
procedure Button1Click(Sender: TObject);
procedure CallMeByName(Sender: TObject);
private
procedure ExecMethod(OnObject: TObject; MethodName: string);
public
{ Public declarations }
end;
var
Form1: TForm1;
type
TExec = procedure of object;
implementation
uses
unit2;
{$R *.dfm}
{................................................................ ExecMethod }
procedure TForm1.ExecMethod(OnObject: TObject; MethodName: string);
var
Routine: TMethod;
Exec : TExec;
begin
Routine.Data := Pointer(OnObject);
Routine.Code := OnObject.MethodAddress(MethodName);
if not Assigned(Routine.Code) then
Exit;
IF not Assigned(Routine.Code) then
Exit;
Exec:= TExec(Routine);
Exec;
end;
{.............................................................. CallMeByName }
procedure TForm1.CallMeByName(Sender: TObject);
begin
ShowMessage('Hello Delphi!');
end;
{.............................................................. Button1Click }
procedure TForm1.Button1Click(Sender: TObject);
begin
ExecMethod(Form1, 'CallMeByName');
end;
end.
//fonte:http://www.planetadelphi.com.br/dica/7469/-usando-variaveis-do-tipo-tprocedure-para-chamadas-pelo-nome-
Gostei + 0
31/08/2010
Lorena
Lorena achei o seguinte: Ve se ajuda você.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, JvExButtons, JvButtons, DB, DBClient,
wwclient, JvDataSource, Grids, DBGrids, JvgDBGrid, JvExControls,
JvXPCore, JvXPButtons;
type
TForm1 = class(TForm)
Button1: TJvHTButton;
procedure Button1Click(Sender: TObject);
procedure CallMeByName(Sender: TObject);
private
procedure ExecMethod(OnObject: TObject; MethodName: string);
public
{ Public declarations }
end;
var
Form1: TForm1;
type
TExec = procedure of object;
implementation
uses
unit2;
{$R *.dfm}
{................................................................ ExecMethod }
procedure TForm1.ExecMethod(OnObject: TObject; MethodName: string);
var
Routine: TMethod;
Exec : TExec;
begin
Routine.Data := Pointer(OnObject);
Routine.Code := OnObject.MethodAddress(MethodName);
if not Assigned(Routine.Code) then
Exit;
IF not Assigned(Routine.Code) then
Exit;
Exec:= TExec(Routine);
Exec;
end;
{.............................................................. CallMeByName }
procedure TForm1.CallMeByName(Sender: TObject);
begin
ShowMessage('Hello Delphi!');
end;
{.............................................................. Button1Click }
procedure TForm1.Button1Click(Sender: TObject);
begin
ExecMethod(Form1, 'CallMeByName');
end;
end.
//fonte:http://www.planetadelphi.com.br/dica/7469/-usando-variaveis-do-tipo-tprocedure-para-chamadas-pelo-nome-
Então Leonardo, tirei por base este exemplo que me passou, só que com ele só consigo executar procedures e não consigo executar funcoes com parametros, entendeu?
Gostei + 0
01/09/2010
Lorena
Fique com Deus e muito obrigada a todos!!!
Ass: Lorena
Gostei + 0
01/09/2010
Carlos Mazzi
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)