RaiseEvent on C sharp

458 Views Asked by At

I know there is a lot of information about RaiseEvents on internet, but I can't understand them, somebody can help me with a simple example on C#.

Thanks very much.

1

There are 1 best solutions below

1
mohsen On BEST ANSWER

Insert this in your class

public event EventHandler<string> MessageHasSent;
public void SendMessage(string message)
{
    EventHandler<string> ms =  MessageHasSent;
    if (ms!= null)
    {
         ms(this,message);
    }
}

And in every where in your class that you want Raise this event. For example this will raise event when an error occurred

try
{
}
catch ( Exception ex)
{
    SendMessage("error occurred :"+ex.Message);
}

And use it like other events