unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
  first,second:integer;
begin
 if novalue(edit1) or novalue(edit2) then
 exit
 else
 begin
    first:=strtoint(edit1.text);
    second:=strtoint(edit2.text);
    edit3.text:=inttostr(first+second);
 end;end;function novalue(testedit:tedit):boolean;
begin
if testedit.text='' then
begin
result:=true;
showmessage('请输入数字');
end
else
result:=false;
end;end.
以上是我想计算两个edit.text相加的问题,函数novalue是判断edit 中是否有数值,如果没有数值提示出错,由数值则进行计算。函数我已经写好了,如果把novalue放到button方法的前面,则不出错,并能执行,但是如果放到后面,就出错,我知道在使用这个自定义的函数之前一定要声明一下,问题就是我不知道应该把function novalue(testedit:tedit):boolean;放在那里进行声明,我放在public里好像不对,应该放到哪里?

解决方案 »

  1.   

    试一下在implementation的上面吧
    这样function novalue(testedit:tedit):boolean;implementation
      

  2.   

    对,放到implementation前面就可以了,或者把这个方法作成这个窗体类的成员方法。
      

  3.   

    //对,放到implementation前面就可以了,或者把这个方法作成这个窗体类的成员方法。怎么做成窗体类的成员?能不能像细说一下?
      

  4.   

    这一改就可以了。
    type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        Button1: TButton;
        Edit3: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function novalue(testedit:tedit):boolean;
      public    { Public declarations }
      end;function TForm1.novalue(testedit:tedit):boolean;
    begin
    if testedit.text='' then
    begin
    result:=true;
    showmessage('请输入数字');
    end
    else
    result:=false;
    end;