Home > .Net, C# > Fire Custom event with custom EventArgs

Fire Custom event with custom EventArgs

Step 1- Create Custom EventArgs

public class ExtendedEventArgs : EventArgs
{
public string Message { get; set;}
public ExtendedEventArgs(string message)
{
Message = message;
}
}

Step 2 – Create Delegate to handle Which will accept this argument

public delegate int ExtendedEvent(object sender, ExtendedEventArgs e);

Step 3 – Create and attach this delegate to event

public event ExtendedEvent OnExtendEvent;

Step 4 – Attach a method to delgate

this.OnExtendEvent += ExtendedEventRaised;

Step 5 – Create handler method to get requried parameter in EventAgrs

private int ExtendedEventRaised(object sender, ExtendedEventArgs e)
{
return e.Message.Length;
}

Step 6 – Fire event on your condition

if (OnExtendEvent != null)
OnExtendEvent(null, new ExtendedEventArgs(“Saresh”));

Advertisements
Categories: .Net, C#
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: