我是新手,如何在程序中自定义一个函数啊?具体该怎么做?比如说我要定义一个函数,功能是将一个字符串加上另一个字符串组成一个新串返回

解决方案 »

  1.   

    现在单元的接口部分申明一个函数,例如
    function ConcatString(Str1,Str2:String):String;
    注意,这个函数不是例程,则把这个申明放到类定义里面然后在实现部分定义实现代码,如下:
    function ConcatString(Str1,Str2:String):String;
    begin
      if Length(Str1)>0 then
        if Length(Str2)>0 then
          Result:=Str1+Str2
        else
          Result:=''
      else
        Result:=Str2;
    end;
      

  2.   

    他总是提示我下面的错误,是怎么回事
    [Error]Unit1.pas[25]:Unsatisfied forward or external declaration:'TForm1.MyFun'
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons, ExtCtrls, DB, ADODB,Inifiles;type
      TForm1 = class(TForm)
        Panel1: TPanel;
        Panel2: TPanel;
        Panel3: TPanel;
        BitBtn1: TBitBtn;
        BitBtn2: TBitBtn;
        Label1: TLabel;
        Label2: TLabel;
        Edit1: TEdit;
        Edit2: TEdit;
        CheckBox1: TCheckBox;
        Image1: TImage;
        ADOQuery1: TADOQuery;
    //**********************************************************
    //可在此声明
    //*********************************************************
        function sumstring(str1,str2:string):string;//返回两个字符串的和
      private
    //**********************************************************
    //也可在此声明,但只能在本单元使用
    //*********************************************************    { Private declarations }
      public
    //**********************************************************
    //也可在此声明,整个工程都能使用,在别的单元也能调用
    //*********************************************************    { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2, Unit4, Unit3;//这里写名其他单元后,就可以引用其他单元的函数{$R *.dfm}/////////////////////////////////////////////////////////////////////////
    //在此写具体的函数内容
    /////////////////////////////////////////////////////////////////////////
    function TForm1.sumstring(str1,str2: string): string;
    begin
      result:=str1+str2;
    end;
    ..
    .
    ..
    ..