函数指针的问题。(100分)

Z

zw84611

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何使用函数指针,如我想实现以下功能:
type
TForm1 = class(TForm)
......
public
{ Public declarations }

function CheckMP3(s:string):boolean;
end;

function TForm1.CheckMP3(s:String):boolean;
begin
if pos('.mp3',lowercase(s))<>0 then Result:=true else Result:=false;
end;

procedure TForm1.BtnClick(Sender: TObject);
var CheckFunc: function(s:string):boolean;
begin
@CheckFunc:=MethodAddress('CheckMP3')

if CheckFunc('1.mp3') ShowMessage('ok');
end;
但这样不行,请问该怎么做?
 
type
TForm1 = class(TForm)
......
public
{ Public declarations }

[red]// function CheckMP3(s:string):boolean;[/red]
end;

[red]function CheckMP3(s:string):boolean;[/red]

function TForm1.CheckMP3(s:String):boolean;
begin
if pos('.mp3',lowercase(s))<>0 then Result:=true else Result:=false;
end;

procedure TForm1.BtnClick(Sender: TObject);
var CheckFunc: function(s:string):boolean;
begin
@CheckFunc:=MethodAddress('CheckMP3')

if CheckFunc('1.mp3') ShowMessage('ok');
end;
试试
 
type
TForm1 = class(TForm)
......
//public // 这改了
published
{ Public declarations }

function CheckMP3(s:string):boolean;
end;

function TForm1.CheckMP3(s:String):boolean;
begin
if pos('.mp3',lowercase(s))<>0 then Result:=true else Result:=false;
end;

procedure TForm1.BtnClick(Sender: TObject);
var CheckFunc: function(s:string):boolean of object
// 这里修改了
begin
//@CheckFunc:=MethodAddress('CheckMP3')

TMethod(CheckFunc).Code :=MethodAddress('CheckMP3')
// 这里
TMethod(CheckFunc).Data := self ;
if CheckFunc('1.mp3') then ShowMessage('ok');// 这里
end;
 
to darkiss
你这样也不行的
 
type
tcheck = function(s:string):boolean;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }

public
{ Public declarations }
published
function CheckMP3(s:string):boolean;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var CheckFunc: tcheck;
begin
CheckFunc := methodaddress('CheckMP3');
if CheckFunc('1.mp3') then ShowMessage('ok');

end;

function TForm1.CheckMP3(s: string): boolean;
begin
if s = '' then result := false
else result := true;
end;

end.
 
type
TForm1 = class(TForm)
......
public
{ Public declarations }

[red]// function CheckMP3(s:string):boolean
[/red]end;

[red]function CheckMP3(s:string):boolean;[/red]
[red]function CheckMP3(s:String):boolean;[/red]
begin
if pos('.mp3',lowercase(s))<>0 then Result:=true else Result:=false;
end;

procedure TForm1.BtnClick(Sender: TObject);
var CheckFunc: function(s:string):boolean;
begin
@CheckFunc:=MethodAddress('CheckMP3')

if CheckFunc('1.mp3') ShowMessage('ok');
end;
这样就能过了
 
procedure TForm1.Button1Click(Sender: TObject);
var
CheckFunc: function (s:string): boolean of Object;
begin
CheckFunc := CheckMP3;
if CheckFunc('1.mp3') then
ShowMessage('ok');
end;
 
to yxyyyy:不行。
 
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
published //***************************************
function CheckMP3(s: String): boolean;
end;

function TForm1.CheckMP3(s:String):boolean;
begin
Result:=pos('.mp3',lowercase(s))<>0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
CheckFunc: function(s:string):boolean of object;
begin
@CheckFunc:=MethodAddress('CheckMP3');
if CheckFunc('1.mp3') then
ShowMessage('ok');
end;


Delphi Help:

TObject.MethodAddress
Returns the address of a published method.
class function MethodAddress(const Name: ShortString): Pointer;
Description
MethodAddress is used internally by the streaming system. When an event property is read
from a stream, MethodAddress converts a method name, specified by Name, to a pointer
containing the method address. There should be no need to call MethodAddress directly.

If Name does not specify a published method for the object, MethodAddress returns nil.
~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~

自己看看帮助就那么难吗???
 
多谢各位。关键是 published,多谢了。
 
不好意思,我结束的时候还没看到creation-zy的帖子。
 
[?]
[blue]如果是在两个单元文件中的函数要用指针调用那又该怎样来操纵?[/blue]
(主窗体单元中调用子单元函数,也用MethodAddress行吗?)
[?]
 
顶部