|
-
May 7th, 2006, 10:12 AM
#1
Thread Starter
PowerPoster
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.
-
May 7th, 2006, 02:23 PM
#2
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
-
May 7th, 2006, 02:52 PM
#3
Thread Starter
PowerPoster
Re: Raising events in various places
thanks, ill give that a shot.... when i sorted out this other uber problem.... lol
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|