自定义函数的声明以及调用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
procedure Edit1Exit(Sender: TObject);
function isnum():boolean;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function isnum(str:string):boolean;
var
i:integer;
begin
for i:=1 to length(str) do
if not (str[i] in [ '0 '.. '9 ']) then
begin
result:=false;
exit;
end;
result:=true;
end;
procedure TForm1.Edit1Exit(Sender: TObject);
var
m:single;
n:integer;
str1:string;
begin
str1:=trim(edit1.Text);
{
for n:=1 to length(str) do
begin
if not (str[n] in [ '0 '.. '9 ']) then
edit1.clear
else
begin
m:=strtofloat(trim(edit1.Text));
Edit1.Text := format( '+%.2f ', [m]);
end;
}
if isnum(str1) then
begin
m:=strtofloat(trim(edit1.Text));
Edit1.Text := format( '+%.2f ', [m]);
end
else
Edit1.clear;
end;
end.
帮忙看看自定义的函数声明以及调用在那里有问题啊
[Error] Unit1.pas(47): Too many actual parameters
[Error] Unit1.pas(14): Unsatisfied forward or external declaration: 'TForm1.isnum '
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas '
[解决办法]
函数和方法是有区别的
方法是声明在类中的函数,访问需要通过类
而函数则是声明在单元这个级别,访问通过单元
type
TForm1 = class(TForm)
//。。
function isnum():boolean;
这样isnum被声明在TForm1中,就成了方法
function TForm1.isnum(str:string):boolean;
// ~~~~~~~ //执行部分就得加上类名
var
i:integer;
begin
//...
或者,如果声明成函数就这样
type
TForm1 = class(TForm)
//...
end;
var
Form1: TForm1;
function isnum():boolean; /// < < < <移出TForm1区
implementation
{$R *.dfm}
function isnum(str:string):boolean;
//...
[解决办法]
function isnum(str:string):boolean;
改成
function TForm1.isnum(str:string):boolean;
这么简单都不懂,看书去!
[解决办法]
function isnum():boolean;
函数声明的时候也要把参数加进去,(str:string)给丢了^_^
------解决方案--------------------
我了一下你的代,改成function TForm1.isnum(str:string):boolean;是可以的呀,有呀!
[解决办法]
呵呵,
function isnum(str:string):boolean;
改成
function TForm1.isnum(str:string):boolean;
这么简单都不懂,看书去!