?
Solved

Is the idea strange?

Posted on 2003-03-13
7
Medium Priority
?
197 Views
Last Modified: 2010-04-03
I declare a class of TObject and add a member variable to it, the member variable has some event handlers. I want to reuse the event handler in the  class, but I don't know what to do. For instance, I need something like:

type
    TMyClass = class(TObject)
    private
        FSock:TCustomWinSocket;
    protected
    public
        procedure OnSockAccept(...);
        procedure OnSockRecv(...);
    end;

I need that the OnSockAccept and OnSockRecv is just FSock's relative event handlers that can be triggered by FSock;
0
Comment
Question by:liyuncheng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:Stefaan
ID: 8134725
Hi,

Could you be a little bit more detailed about what you actually want to do ? If I got it right you just want the event handlers for the FSock available.  To be able to help you we will need more detailed information on what you actually want to do.

Regards,


Stefaan
0
 
LVL 2

Expert Comment

by:j42
ID: 8134973
Hi,
just some thoughts:



type
  TFnWhatever = procedure(...) of object;

type
  TMyClass = class(TObject)
  private
    FSock:TCustomWinSocket;

    procedure FOnSockAccept(...);
    procedure FOnSockRecv(...);
  protected
  public
    OnSockAccept: TFnWhatever;
    OnSockRecv: TFnWhatever;
  end;

...

  // Maybe in constructor  
  FSock.OnSockAccept := FOnSockAccept;
  FSock.OnSockRecv:= FOnSockRecv;

...

procedure TMyClass.FOnSockAccept(...);
begin
  if Assigned(OnSockAccept) then OnSockAccept(...);
end;



Regards
J
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8135202
you must declare those events as properties and write getter and setter methods:

type
   TMyClass = class(TObject)
   private
     FSock: TCustomWinSocket;
     function ReadSockAccept: TSocketNotifyEvent;
     function ReadSockRecv: TSocketNotifyEvent;
     procedure WriteSockAccept(Value: TSocketNotifyEvent);
     procedure WriteSockRecv(Value: TSocketNotifyEvent);
   public
     property OnSockAccept: TSocketNotifyEvent read ReadSockAccept write WriteSockAccept;
     property OnSockRecv: TSocketNotifyEvent read ReadSockRecv write WriteSockRecv;
   end;

implementation


function TMyClass.ReadSockAccept: TSocketNotifyEvent;
begin
  Result := FSock.OnAccept;
end;

function TMyClass.ReadSockRecv: TSocketNotifyEvent;
begin
  Result := FSock.OnRecv;
end;

procedure TMyClass.WriteSockAccept(Value: TSocketNotifyEvent);
begin
  FSock.OnAccept := Value;
end;

procedure TMyClass.WriteSockRecv(Value: TSocketNotifyEvent);
begin
  FSock.OnRecv := Value;
end;

wbr, mo.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
steve_hsk earned 375 total points
ID: 8135330
Hi liv ...

The idea is not strange at all, and creating VCL objects dynamically and binding them to events at run-time is something you'll do more and more as you become comfortable with the idea.

However, TCustomWinSocket has no Accept or Recv events, and so you would have to write these yourself, in which case let us know, and I'll post another example.

Another choice you have is to develop the WinSock code yourself, without using Borland's API, and interface with the Winsock API directly with calls to WSAStartup, WSAListen, WSASend, etc, etc ... let me know if you want a simple example.

To demonstrate creating a simple socket at run-time, I'll use TServerSocket, as this seems to match TCustomWinSocket and the Server Accept funcitonality u have :

TYPE TMyClass = CLASS(TObject)
PRIVATE
   FSock : TServerSocket;
PUBLIC      
   CONSTRUCTOR Create;
   DESTRUCTOR  Destroy;
   PROCEDURE   OnAccept(...); // arg list must match TServerSocket.'OnEvent' args
   PROCEDURE   OnListen(...);
END;

CONSTRUCTOR TMyClass.Create;
BEGIN
   FSock := TServerSocket.Create(...);
   FSock.OnListen := OnListen;
   FSock.OnAccept := OnAccept;
   // etc, etc
END;

DESTRUCTOR TMyClass.Destroy;
BEGIN
   FSock.Free;
END;

PROCEDURE TMyClass.OnSockAccept(...);
BEGIN

PROCEDURE TMyClass.OnSockRecv(...);
BEGIN

etc etc

Hope at least some of this helps,
STeve
0
 
LVL 2

Expert Comment

by:j42
ID: 8142985
Hi Steve,

you made it. Congratulations!

:-)
J
0
 
LVL 2

Expert Comment

by:steve_hsk
ID: 8143363
ha ha ... hey J ... how's tricks :-) ?
0
 
LVL 2

Expert Comment

by:j42
ID: 8144772
> how's tricks
Huh... I've got to look up this phrase in my dictonary!
Well, I'm fine. Thanks for asking.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question