سؤال

I am trying to run a procedure from another form, but it won't work.
This is the error:
Unsatisfied forward or external declaration: 'TForm1.SendTextToSocket'

I am try to put the procedure under "Public"

This is my procedure:

procedure SendTextToSocket(Socket: TCustomWinSocket; S: AnsiString);
var
  Data: PAnsiChar;
  DataLen, Sent: Integer;
begin
sleep(1);
  Data := PAnsiChar(S);
  DataLen := Length(S);
  while DataLen > 0 do
  begin
    Sent := Socket.SendBuf(Data^, DataLen);
    if Sent = SOCKET_ERROR then
    begin
      if WSAGetLastError <> WSAEWOULDBLOCK then
        Exit;
    end
    else if Sent = 0 then
    begin
      Exit;
    end else
    begin
      Inc(Data, Sent);
      Dec(DataLen, Sent);
   end;
  end;
end;

And this is what i set by Public:

procedure SendTextToSocket(Socket: TCustomWinSocket; S: AnsiString);

What i am doing wrong?

هل كانت مفيدة؟

المحلول

In definition of your procedure, write your form class name + dot. For example if your form's class is TForm1 write it this way:

procedure TForm1.SendTextToSocket(Socket: TCustomWinSocket; S: AnsiString);

However, in declaration under public, you should do it the way you've done.

Now, calling this method from another class is easy. Just add its unit to the uses section of the user unit. Then, call it like this: Form1.SendTextToSocket

Here's an example:

Main Form Unit

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure MyMethod(Param1:Integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyMethod(Param1:Integer);
begin
  //do something
end;

end.

Second Form Unit

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
  Form1.MyMethod(5);
end;

end.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top