Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,795 members, 7,956,036 topics. Date: Sunday, 22 September 2024 at 09:59 PM |
Nairaland Forum / Science/Technology / Programming / I Have A Problem With Raise Event In C# (9416 Views)
Creating UWP Application In C# Or C++ (xaml) / Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD / [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function (2) (3) (4)
(1) (Reply)
I Have A Problem With Raise Event In C# by luckyCO(m): 4:01pm On Mar 18, 2007 |
Am building a class Called MailClass which I need to Raise event called [B]ShowInfo [/b] each time I have any Messages. In Vb.Net You simple go to class Modules and write Public Event ShowInfo(ByVal MsgReport As String). To send any Messagse I would write RaiseEvent Showinfor("hai" Then to consume it in a form I would write Dim WithEvents ReportError As MailClass Automatically it will gives me a procedure called Public Sub MsgReport _AnyError(ByVal ReportError As String) MsgBox ReportError End Sub Please How can I do all these in C# |
Re: I Have A Problem With Raise Event In C# by Fdeveloper(m): 4:21pm On Mar 21, 2007 |
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! |
(1) (Reply)
The Travails Of Junior Developers / Definitive Link To A List Of More Than 500 Free Ebooks. / Dare Obasanjo (OBJ Junior): Microsoft IT Developer
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16 |