C# Standard Event Pattern
In this chapter you will learn:
- What is C# Standard Event Pattern
- Step 1: subclass EventArgs
- Step 2: define a delegate for the event
- Step 3:Fire event
- Example for standard event pattern
Description
The .NET Framework defines a standard pattern for writing events.
Step 1
System.EventArgs is a predefined Framework class with no members. EventArgs is a base class for conveying information for an event.
We can subclass EventArgs to convey the old and new values when a ValueChanged event is fired:
public class ValueChangedEventArgs : System.EventArgs
{/* w w w .j av a 2s . c om*/
public readonly decimal LastValue;
public readonly decimal NewValue;
public ValueChangedEventArgs (decimal lastValue, decimal newValue)
{
LastValue = lastValue;
NewValue = newValue;
}
}
It typically exposes data as properties or as read-only fields.
Step 2
With an EventArgs subclass in place, the next step is to choose or define a delegate for the event.
The Framework defines a generic delegate called System.EventHandler<> we can use.
public class Rectangle
{/*w ww. j a va 2 s .co m*/
...
public event EventHandler<ValueChangedEventArgs> ValueChanged;
}
Step 3
You write a protected virtual method that fires the event.
The name must match the name of the event, prefixed with the word On, and then accept a single EventArgs argument:
/* ww w . j a v a2 s . co m*/
public class Rectangle {
public event EventHandler<ValueChangedEventArgs> ValueChanged;
protected virtual void OnValueChanged (ValueChangedEventArgs e)
{
if (ValueChanged != null) ValueChanged (this, e);
}
}
This provides a central point from which subclasses can invoke or override the event.
Example
Here's the complete example:
using System;
public class ValueChangedEventArgs : EventArgs
{// w w w . jav a2s . c o m
public readonly decimal LastValue;
public readonly decimal NewValue;
public ValueChangedEventArgs (decimal lastValue, decimal newValue)
{
LastValue = lastValue; NewValue = newValue;
}
}
public class Rectangle
{
string symbol;
decimal myValue;
public Rectangle (string symbol) {this.symbol = symbol;}
public event EventHandler<ValueChangedEventArgs> ValueChanged;
protected virtual void OnValueChanged (ValueChangedEventArgs e)
{
if (ValueChanged != null) ValueChanged (this, e);
}
public decimal MyWidth
{
get { return myValue; }
set
{
if (myValue == value) return;
OnValueChanged (new ValueChangedEventArgs (myValue, value));
myValue = value;
}
}
}
class Test
{
static void Main()
{
Rectangle stock = new Rectangle ("A");
stock.MyWidth = 2;
stock.ValueChanged += stock_ValueChanged;
stock.MyWidth = 3;
}
static void stock_ValueChanged (object sender, ValueChangedEventArgs e)
{
Console.WriteLine (e.NewValue);
Console.WriteLine (e.LastValue);
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What
- How to use enum type
- Example for create an enum type
- Print out the details of any enum
- How to create enum type
- Is enum value defined
- How to use enum with switch statement
- How to get all names for an enum
- How to create enum from string
- How to check if a string value is defined