views:

62

answers:

2

I am trying to create a Tclientsocket at runtime but I can't assign the events.

I use

var
  cs:TCLIENTSOCKET;

procedure OnReadx;
begin

end;

procedure intsok;
begin
  cs:=Tclientsocket.create(nil);
  cs.OnRead:=OnReadx;
end;

It doesn't work. what is the right way to do this?

+3  A: 

The problem is that the TClientSocket class requires that the event handlers for its various events to be method pointers (they must belong to some object), as opposed to regular procedures.

Solved it!

opc0de
You'll find that's true for *all* classes' event properties, not just TClientSocket's.
Rob Kennedy
+1  A: 

and the event is declared like this

TSocketNotifyEvent = procedure (Sender: TObject; Socket: TCustomWinSocket) of object;

so you've wrote a function with those parameters, e.g

procedure OnReadx(Sender: TObject; Socket: TCustomWinSocket);

and assign it like in your code:

cs.OnRead:=OnReadx;

best regards,

Radu Barbu
really, i don't understand why some one set a -1 to a good answer. it doesn't matter if i get -1 for all my questions, but it should exist an mechanism to provide a reason before you get a -1. opc0de, hope that the answer helped you ;)
Radu Barbu
i already solved it radu dar mersi ;)
opc0de
I voted this down, and as I was composing a comment to explain why I didn't like this answer, I read the answer more carefully and realized that the answer was not as egregious as I originally thought, so I took back my vote within the time limit, so there is no visible record of my vote. See for yourself. I will not vote it up, though, because it does not mention that the subroutine needs to be a method instead of a standalone procedure.
Rob Kennedy