在一个Unit里创建了一个ClientSocket控件(只有.pas文件没有dfm),      声明了一个过程 procedure ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);在给那个动态创建了的ClientSocket控件的OnRead 赋值的时候报错:"method pointer and regular procedure"这样的错误(gSocket.OnRead:=ClientSocketRead)。
请问怎样解决?

解决方案 »

  1.   

    unit uCheck;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ScktComp,IniFiles;var
      gSocket:TClientSocket;
      
      function CreateSocket:Boolean;
      function SendCommand:Boolean;
      function GetParth:string;
      procedure ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);implementationfunction CreateSocket:Boolean;
    var
      IsCreate:Boolean;
    begin
      IsCreate:=true;
      try
        gSocket:= TClientSocket.Create(nil);
        gSocket.Host:='140.200.0.11';
        gSocket.Port:=10899;
        gSocket.OnRead:=ClientSocketRead;
    //    gSocket.Open;
      except
    //    
        gSocket.Free;
        IsCreate:=false;
      end;
      If IsCreate then
        Result:=true
      else
        Result:=false;
    end;function SendCommand:Boolean;
    begin
      If gSocket<>nil then
      begin
        If not gSocket.Active then
          gSocket.Open;
        gSocket.Socket.SendText('PleaseSendVersionInfo&'+GetParth);
      end;
    end;function GetParth:string;
    var
      IniFile:TIniFile;
      strSour:string;
    begin
      strSour:='';
      IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Init.ini');
      try
        strSour := IniFile.ReadString('ServerPath', 'Path', 'c:\Project1.exe');
      finally
        IniFile.Free;
      end;
      Result:=strSour;
    end;procedure ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);
    var
      str:string;
    begin
      str:=Socket.ReceiveText;
    end;
      
    end.
      

  2.   

    这是一个客户端的Unit ,这句 gSocket.OnRead:=ClientSocketRead;编译不通过。
      

  3.   

    ClientSocketRead过程一定要隶属于一个对象,不能这样直接申明使用。
      

  4.   

    建议先多学点相关基础的英文单词,OnRead是method pointer,和ClientSocketRead这样的regular procedure数据类型当然不一样。其实编译器提示已经把错误明白的告诉了你。
      

  5.   

    OnRead事件的原型是:
    type TSocketNotifyEvent = procedure (Sender: TObject; Socket: TCustomWinSocket) of object;
    后面有of object,这就说明和longhuazhen所说的一样,
    ClientSocketRead过程一定要隶属于一个对象,才满足OnRead事件原型的定义。
      

  6.   

    大至就如下面这样unit uCheck;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ScktComp,IniFiles;type
      Txxx=class(TObject)
    private
      gSocket:TClientSocket;
    public
      function CreateSocket:Boolean;
      function SendCommand:Boolean;
      function GetParth:string;
      procedure ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);
    end;implementationfunction Txxx.CreateSocket:Boolean;
    var
      IsCreate:Boolean;
    begin
      IsCreate:=true;
      try
        gSocket:= TClientSocket.Create(nil);
        gSocket.Host:='140.200.0.11';
        gSocket.Port:=10899;
        gSocket.OnRead:=ClientSocketRead;
    //    gSocket.Open;
      except
    //    
        gSocket.Free;
        IsCreate:=false;
      end;
      If IsCreate then
        Result:=true
      else
        Result:=false;
    end;function Txxx.SendCommand:Boolean;
    begin
      If gSocket<>nil then
      begin
        If not gSocket.Active then
          gSocket.Open;
        gSocket.Socket.SendText('PleaseSendVersionInfo&'+GetParth);
      end;
    end;function Txxx.GetParth:string;
    var
      IniFile:TIniFile;
      strSour:string;
    begin
      strSour:='';
      IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Init.ini');
      try
        strSour := IniFile.ReadString('ServerPath', 'Path', 'c:\Project1.exe');
      finally
        IniFile.Free;
      end;
      Result:=strSour;
    end;procedure Txxx.ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);
    var
      str:string;
    begin
      str:=Socket.ReceiveText;
    end;
      
    end.
      

  7.   

    如果有窗体,我就不用动态创建了
    ------------------------------
    窗体=类?你是这么理解的?这么说吧,你这种程序出现了全局函数本身就是非常糟糕的设计。而且类方法本身就是一种数据类型,就算要这里要用全局函数也需要自己填充一个TMethod。