刚学习delphi的新人,碰到一个问题。以往用的是C++,函数都是先声明,后定义。在类定义的public或private区声明,即可在其它函数里使用。可在delphi里却实现不了这点,郁闷。
例如:
unit Score;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComObj, StdCtrls, xmldom, XMLIntf, msxmldom, XMLDoc;type
  TForm1 = class(TForm)
    btnWord: TButton;
    btnExcel: TButton;
    btnPowerPoint: TButton;
    dlgOpen: TOpenDialog;
    procedure btnWordClick(Sender: TObject);   private
    { Private declarations }  public
     procedure search();
    { Public declarations }  end;var
  Form1: TForm1;
  WordApp: variant;
  WordDoc: variant;
  WordRange: variant;
  xmlDocument: IXMLDocument;
  Root: IXMLNode;
  Parent_Node: IXMLNode;
  Child_Node: IXMLNode;implementation
......procedure wordconnect(.....);
var
.....
begin
.......search();......................第112行
end;procedure search();
var
....
begin
.....
end;编译就提示第112行处search是无定义。将search放于type区后,同样提示错误,难道delphi中必需要先定义后使用吗?请大侠指点。

解决方案 »

  1.   

    既然你以往用过C++,那应该知道在C++里::是什么意思的。在Delphi里,是这样的:procedure 类名.方法名;
      

  2.   

    C++class TForm1
      {
       //....
      int func();
      };int TForm1::func()
    {
     //...
    }------------------------Delphitype
      TForm1 = class(TForm)
      function func():integer;
      end;//...function TForm1.func():integer;
    begin
    //...
    end;
      

  3.   

    还想对楼主说:你的贴子的题目“如何到全局过程声明”和贴子内容不太相符。
    如果你仅仅是想在单元里声明一个全局过程(而不是类方法)然后使用它,记得要遵循先声明后使用的原则。似乎C++也是这样的。形式一:
    interface
      procedure ProcA;//在单元的接口部分声明一下
    //...
    implementationprocedure ProcB;
    begin
      ProcA;//使用
    end;procedure ProcA;//定义可以随便放
    begin
      ///...
    end;形式二:implementationprocedure ProcA;//先声明兼定义
    begin
      ///...
    end;procedure ProcB;
    begin
      ProcA;//再使用
    end;
    形式三:implementationprocedure ProcA; forward;procedure ProcB;
    begin
      ProcA;
    end;procedure ProcA;
    begin
      ///...
    end; 
      

  4.   

    你没界定类的方法,这和c++是一样的,只不过界定符不同,在c++中是::,而d中是.而已
    void TForm1::Search()procedure TForm1.Search();
      

  5.   

    按Ctrl +Shift + c,就可以用了.