C# Standard Event Pattern

In this chapter you will learn:

  1. What is C# Standard Event Pattern
  2. Step 1: subclass EventArgs
  3. Step 2: define a delegate for the event
  4. Step 3:Fire event
  5. 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:

  1. What
  2. How to use enum type
  3. Example for create an enum type
  4. Print out the details of any enum
  5. How to create enum type
  6. Is enum value defined
  7. How to use enum with switch statement
  8. How to get all names for an enum
  9. How to create enum from string
  10. How to check if a string value is defined
Home »
  C# Tutorial »
    C# Types »
      C# Event
C# Events
C# Standard Event Pattern