我手头有一本书,讲function时一笔带过。
我做了个function,如下:
function play(const url:string):boolean;
begin
text1.text:='string'; //error
result:=true;
end;请问为什么显示text1没有声明呢。谁给我讲解一下function的用法,给100百分了。谢谢。
我做了个function,如下:
function play(const url:string):boolean;
begin
text1.text:='string'; //error
result:=true;
end;请问为什么显示text1没有声明呢。谁给我讲解一下function的用法,给100百分了。谢谢。
begin
text1.text:='string'; //error
result:=true;
end;由于你在外面,不能引用类的成员,比如form中的text1控件,而需要将他写成类的成员函数,或者用
class function
函数声明在form类里面蹴可以了,改正后
function TForm1.play(const url:string):boolean;//将它定义为类的函数
begin
Form1.text1.text:='string'; //play不是在你的窗体类内定义的,所以不能默认,要写上form1的名
result:=true;
end;
begin
text1.text:='string'; //error
result:=true;
end;//error:play Undeclared identifier:'play'
如果把function play写form中,play调用form 的text1没问题,
如果text1是全局变量的话,没问题!
在前面还是定义
function play(const url:string):boolean
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
text1: TEdit;
function play( url:string):boolean;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
play('ssss');
end;
function TForm1.play( url:string):boolean;
begin
text1.text:=url;
result:=true;
end;
end.
function play( url:string):boolean; //声明function TForm1.play( url:string):boolean;//实现
begin
text1.text:=url;
result:=true;
end;