How to Create an Event for a C Sharp Class

Create the Event Arguments Class: Decide what you want to communicate with your event subscribers (other programmers who will attach event handlers to your event)., Declare the event delegate., Declare the event itself in the containing class: use...

6 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: Create the Event Arguments Class: Decide what you want to communicate with your event subscribers (other programmers who will attach event handlers to your event).

    For example, if your event is to notify developers when a value changes, you might want to tell them the old value and the new one.

    If you have nothing to communicate to subscribers, use the System.EventArgs class and skip this step.

    Create a public class named EventNameEventArgs where EventName is the name of your event Inherit the EventNameEventArgs class from System.EventArgs Create a protected field for each piece of data you want to communicate with your subscribers.

    For example, in an event that will notify developers of a change in a string value, you might want to tell them the old and the new strings so the fields will look like: protected string oldVal, newVal; Create a public property for each field you created in
    1.4 that has only a get{ return fieldName;} method (where fieldName is the name of the field you created in
    1.4 Create a private empty constructor for the class with no implementation.

    The constructor should look like: private EventNameEventArgs(){} Create a public constructor for the class that takes as many arguments as there is fields/data.

    For example, in a string value change event, the constructor will look like: public EventNameEventArgs(string oldValue, string newValue){oldVal = oldValue; newVal = newValue;}
  2. Step 2: Declare the event delegate.

    If you did not create an event arguments class because there is no data to communicate with subscribers, use the System.EventHandler delegate and skip this step.

    The delegate declaration should look like: public delegate void EventNameHandler(object sender, EventNameEventArgs e)
  3. Step 3: Declare the event itself in the containing class: use the event handler delegate you declared/decided in 2 as the event type.

    The declaration should look like: public event EventNameHandler EventName
  4. Step 4: Declare the event-firing method - a protected virtual method that has exactly the following declaration: protected virtual void OnEventName(EventNameEventArgs e){ if(EventName != null) { EventName(this

    , This is the hardest part.

    You should know when the event you are creating will fire (what areas in your code causes the event to occur) and call the method with the appropriate event arguments class instance.

    For example, in the string value change event, you must see what code can cause this value to change, save its old value before the change, allow the code to change the value, create an event arguments object with the old and new values passed to the constructor and pass the object to the event-firing method.
  5. Step 5: e); } } Use the event arguments class you decided to use in 1

  6. Step 6: Call the event-firing method you declared in 4 whenever the event occurs.

Detailed Guide

For example, if your event is to notify developers when a value changes, you might want to tell them the old value and the new one.

If you have nothing to communicate to subscribers, use the System.EventArgs class and skip this step.

Create a public class named EventNameEventArgs where EventName is the name of your event Inherit the EventNameEventArgs class from System.EventArgs Create a protected field for each piece of data you want to communicate with your subscribers.

For example, in an event that will notify developers of a change in a string value, you might want to tell them the old and the new strings so the fields will look like: protected string oldVal, newVal; Create a public property for each field you created in
1.4 that has only a get{ return fieldName;} method (where fieldName is the name of the field you created in
1.4 Create a private empty constructor for the class with no implementation.

The constructor should look like: private EventNameEventArgs(){} Create a public constructor for the class that takes as many arguments as there is fields/data.

For example, in a string value change event, the constructor will look like: public EventNameEventArgs(string oldValue, string newValue){oldVal = oldValue; newVal = newValue;}

If you did not create an event arguments class because there is no data to communicate with subscribers, use the System.EventHandler delegate and skip this step.

The delegate declaration should look like: public delegate void EventNameHandler(object sender, EventNameEventArgs e)

The declaration should look like: public event EventNameHandler EventName

, This is the hardest part.

You should know when the event you are creating will fire (what areas in your code causes the event to occur) and call the method with the appropriate event arguments class instance.

For example, in the string value change event, you must see what code can cause this value to change, save its old value before the change, allow the code to change the value, create an event arguments object with the old and new values passed to the constructor and pass the object to the event-firing method.

About the Author

J

Janice Gray

Creates helpful guides on practical skills to inspire and educate readers.

50 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: