function GetStr(StrSource,StrBegin,StrEnd:string):string;
var
in_star,in_end:integer;
begin
in_star:=AnsiPos(strbegin,strsource)+length(strbegin);
in_end:=AnsiPos(strend,strsource);
result:=copy(strsource,in_star,in_end-in_star);
end;procedure TForm1.Button1Click(Sender: TObject);
 var mystr,sourcestr:string;
begin
 IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
 try
 sourcestr:=IdHTTP1.Get('http://www.baidu.com');
 except
 Showmessage('网络出错!');
 Exit;
 end;
end;
上面这样的代码就可以运行,但我想把 procedure TForm1.Button1Click(Sender: TObject);改成一个函数或过程,就发生错误了!
把按钮的过程成这样就运行不了
procedure getsourcesrt;
 var mystr,sourcestr:string;
begin
 IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
 try
 sourcestr:=IdHTTP1.Get('http://www.baidu.com');
 except
 Showmessage('网络出错!');
 Exit;
 end;
end;提示错误是
[DCC Error] Unit1.pas(43): E2003 Undeclared identifier: 'IdAntiFreeze1'
[DCC Error] Unit1.pas(43): E2066 Missing operator or semicolon
[DCC Error] Unit1.pas(45): E2003 Undeclared identifier: 'IdHTTP1'
[DCC Error] Unit1.pas(15): E2065 Unsatisfied forward or external declaration: 'TForm1.Button1Click'
[DCC Fatal Error] Project1.dpr(5): F2063 Could not compile used unit 'Unit1.pas'
这个是什么问题,为什么在按钮动作下就可以,我改成函数就行了!!

解决方案 »

  1.   

    如果你的函数中要使用Form上的组件,有两种方法:
    1. 把函数定义成Form类的成员函数
    2. 在要访问的组件对象前面加上Form实例名,如Form1.IdAntiFreeze1
      

  2.   

    procedure getsourcesrt;
     var mystr,sourcestr:string;
    begin
     with Form1 do // {demo}  with AForm do
     begin
     IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
     try
     sourcestr:=IdHTTP1.Get('http://www.baidu.com');
     except
     Showmessage('网络出错!');
     Exit;
     end;
     end
    end;
      

  3.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
      IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls;type
      TForm1 = class(TForm)
        IdHTTP1: TIdHTTP;
        IdAntiFreeze1: TIdAntiFreeze;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function GetStr(StrSource,StrBegin,StrEnd:string):string;
    var
    in_star,in_end:integer;
    begin
    in_star:=AnsiPos(strbegin,strsource)+length(strbegin);
    in_end:=AnsiPos(strend,strsource);
    result:=copy(strsource,in_star,in_end-in_star);
    end;procedure TForm1.Button1Click(Sender: TObject);
     var mystr,sourcestr:string;
    begin
     IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
     try
     sourcestr:=IdHTTP1.Get('http://www.bjfcdt.gov.cn/datacenter/gettvplusinfo.aspx');
     except
     Showmessage('网络出错!');
     Exit;
     end;
     mystr:=GetStr(sourcestr,'&awardnum=','&plusinfo');
     ShowMessage(mystr);
    end;end.
      

  4.   


    1. 把函数定义成Form类的成员函数
    第一点怎么做
      

  5.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
      IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls;type
      TForm1 = class(TForm)
      IdHTTP1: TIdHTTP;
      IdAntiFreeze1: TIdAntiFreeze;
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
      private
      { Private declarations }
      public
      { Public declarations }
      procedure getsourcesrt; //如果只是本单元调用,可以定义在private
      end;var
      Form1: TForm1;implementation{$R *.dfm}function GetStr(StrSource,StrBegin,StrEnd:string):string;
    var
    in_star,in_end:integer;
    begin
    in_star:=AnsiPos(strbegin,strsource)+length(strbegin);
    in_end:=AnsiPos(strend,strsource);
    result:=copy(strsource,in_star,in_end-in_star);
    end;procedure TForm1.Button1Click(Sender: TObject);
     var mystr,sourcestr:string;
    begin
     IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
     try
     sourcestr:=IdHTTP1.Get('http://www.bjfcdt.gov.cn/datacenter/gettvplusinfo.aspx');
     except
     Showmessage('网络出错!');
     Exit;
     end;
     mystr:=GetStr(sourcestr,'&awardnum=','&plusinfo');
     ShowMessage(mystr);
    end;procedure TForm1.getsourcesrt;
    var
    mystr,sourcestr:string;
    begin
     IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
     try
     sourcestr:=IdHTTP1.Get('http://www.baidu.com');
     except
     Showmessage('网络出错!');
     Exit;
     end;
    end;

    end.
    请看以上红色字体的,按说应没问题。
      

  6.   

    再问个问题:
    比如我现在有一个字符数组 mystr:=5,8,7,4,2 怎样把他从小到大排列成一个新的数组newstr(这时的数组是INT型,不是字符)??
      

  7.   

    function maopao(a: array of integer): array;
    var i,j,temp:integer;
    begin
    for i := high(a) downto low(a) do
      begin
      for j := low(a) to i do
      begin
      if a[j]>a[j+1] then
      begin
      temp := a[j];
      a[j] := a[j+1];
      a[j+1] := temp;
      end;
      end;
      end;
    end;
    我这样定义一个函数有问题吗?
    为什么说[DCC Error] Unit1.pas(55): E2029 Identifier expected but 'ARRAY' found
    我把array 改成tStringlist就可以了  
    但我想返回的类型是array
      

  8.   

    (半径为10.00圆心为pointfunction且满足((((匹配单位)是存活的)等于TRUE)and(((匹配单位)是(kof97hero的所有者)的同盟单位)等于FALSE))的所有单位