Results 1 to 3 of 3

Thread: Raising events in various places

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Raising events in various places

    I almost understand the ways of raising events/delegates. All cool stuff. (using .NET 2.0 here)

    Now, since my application will not always have a tcp listener available (user can choose this option), it means that the communication classes wont be instantiated until the user selects that they want to listen to incoming communication.

    When the user selects this, classA is instantiated, which instantiates classB, which instantiates classC..... and so on.

    However, when a certain action/event happens within my application, I want an event to be raised, this event raised should be responded/notifiable to the communication classes only if they are instantiated, or even better, raise the event and the classes will act upon it if they are instantiated.

    the Last class of the communication classes will be the one sending data so the events should be detected there, giving it the data to send.

    How can I achieve this? how can I raise an event and let the instantiated classes know about this? as well as this, if the event is raised and the communication classes are not instantiated, thats fine, it should simply just ignore it.

    using C# 2.0

    thanks!
    Last edited by Techno; May 7th, 2006 at 10:17 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Raising events in various places

    Unless I'm totally misunderstanding you, you would just add event handlers for the events you want to handle after you instantiate the classes. For example, you might create a TCP received event (I'm not familiar with the TCP Listener, so this is all psuedo-code:

    Somewhere in your class, you have this
    Code:
    private void TCP_Received(object sender, eventargs e)
    {
      //Do some TCP Stuff
    }
    Then when you instantiate the class, you add a handler for it.

    Code:
    TCPListener myTCP = new TCPListener ();
    myTCP.TCPReceived += new System.EventHandler(this.TCP_Received);
    So essentially, you have the function ready whether or not the object is using it, and upon instantiating, you just add a handler to it. You can use the same handler for multiple objects if you use the sender parameter and cast the type.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Raising events in various places

    thanks, ill give that a shot.... when i sorted out this other uber problem.... lol

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width