I Have A Problem With Raise Event In C#

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 21, 2009, 12:24 PM
430386 members and 297523 Topics
Latest Member: impressio2
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  I Have A Problem With Raise Event In C#
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: I Have A Problem With Raise Event In C#  (Read 5953 views)
luckyCO
I Have A Problem With Raise Event In C#
« on: March 18, 2007, 04:01 PM »

Am building a class Called MailClass which I need to Raise event called ShowInfo 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#
Fdeveloper (m)
Re: I Have A Problem With Raise Event In C#
« #1 on: March 21, 2007, 04:21 PM »

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!
 Ms .net Core - Mcad-mcsd, E-books In .chm Format  Mcts 70-536 Brain Dumps  Web Based Software Vs Standalone Solution  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.