|
Fdeveloper (m)
|
It's pretty similar to VB as both languages are based on the.NET kernel
First you have to write a class which inherits from EventArgs to act as a container for the parameters of your event as follows:
/// <summary> /// Event handler implementation for a new message /// </summary> public class NewEventArgs : EventArgs { private string message;
/// <summary> /// Event message setter & getter /// </summary> public string m_message { get { return message; } set{ message = value; } }
/// <summary> /// Class constructor /// </summary> /// <param name="message"></param> public NewEventArgs(string message) { m_message = message; return; } }
You then need to declare a delegate method to handle the event as follows:
/// <summary> /// Delegate declaration /// </summary> /// <param name="objectSender"></param> /// <param name="newEventArgs"></param> public delegate void NewEventHandler(object objectSender, NewEventArgs newEventArgs);
Note that both of the above can be in the same source file
The following is an example class which has a method that receives a string and raises an event public ReceiveString { /// <summary> /// New event delegate /// </summary> public event NewEventHandler newEventHandler; /// <summary> /// This method receives an external message from somewhere /// </summary> public string NewMessage(string message) { // Raise a new event and pass the string we received RaiseNewMessageEvent(new NewEventArgs(message)); } /// <summary> /// This method raises a new event /// </summary> /// <param name="newEventArgs"></param> protected virtual void RaiseNewMessageEvent(NewEventArgs newEventArgs) { // It's important to check that there is an active listener // before raising the event if(newEventHandler != null) { // This actually raises the event newEventHandler(this, newEventArgs); } } } Finally, you can now write the class that will consume the event public ConsumeEvent { // Instance of the class that will raise the event public ReceiveString m_receiveString; /// <summary> /// New event delegate /// </summary> public event NewEventHandler newEventHandler; /// <summary> /// Class constructor /// </summary> public ConsumeEvent() { // Define the callback method that will catch the event m_receiveString.newEventHandler += NewMessageEvent(NewEventHandler); } /// <summary> /// Event handler for new message /// </summary> /// <param name="objectSender"></param> /// <param name="newEventArgs"></param> void NewMessageEvent(object objectSender, NewEventArgs newEventArgs) { MsgBox(newEventArgs.getMessage()); return; } } Note that I haven't included any error handling in the code and this should certainly be done before deploying your application in the real world.
One final point is that this is a perfect example of why it is always a good idea to investigate and fully understand what a wizard in the IDE is doing. The aim of a wizard is so speed up your development however it is in your interests to learn and understand the underlying principles involved.
Hope this helps!
|