carlAlex
8/22/2016 - 12:46 PM

C# Carl Event

C# Carl Event

In the class that wants to know when something happens:

A method to handle the event:

        private void TransactionGeneratedHandler(object sender, TransactionGeneratedEventArgs e)
        {
            Transactions.Add(e.TransactionGenerated);
        }
        
And finally a smart place where you actually subscribe (after the sending obj is created):

tGen.TransactionGenerated += TransactionGeneratedHandler;
In the class that raises the event:

public delegate void TranGeneratedHandler(object sender, TransactionGeneratedEventArgs e);
//The event itself is referenced again when subscribing from the desired class
public event TranGeneratedHandler TransactionGenerated;

Then, when you want to raise the event:
RaiseTransactionGeneratedEvent(new TransactionGeneratedEventArgs(result));


Inside the same class, another method:
private void RaiseTransactionGeneratedEvent(TransactionGeneratedEventArgs e)
{
    TransactionGenerated?.Invoke(this, e);
}
Every single event in .NET, whether Microsoft created it or if it was created by someone else, is based on a .NET delegate. Delegates are one of the five types of types included with .NET – class, structure, interface, enumeration, and delegate. At its foundation, delegates do two things:

1.When created, it points to a method (instance or static) in a container (class or structure). For events, it points to an event hander method.

2.It defines exactly the kind of methods that it can point to, including the number and types of parameters and also the return type.

**DELEGATE** Points to a method, and defines exactly the kind of methods it can point to!

public delegate int dgPointer(int a, int b);

class Program
{
  static void Main()
  {
    Adder a = new Adder();
    dgPointer pAdder = new dgPointer(a.Add);    
    int iAnswer = pAdder(4, 3);
    Console.WriteLine("iAnswer = {0}", iAnswer);
    // Returns “iAnswer = 7”
  }
}

public class Adder
{
  public int Add(int x, int y)
  { return x + y; }
}


********
NOTE: All events in .NET are based on delegates.

NOTE: Wherever you want to raise an event, you must also define the event.

NOTE: You must never raise (publish) an event unless at least one object is listening (subscribing) to the event. In other words, the event must not equal null.

NOTE: A Microsoft Best Practice: All events should be defined starting with the word “On”.
********

public class Adder
{
  public delegate void dgEventRaiser();
  public event dgEventRaiser OnMultipleOfFiveReached;
  
  public int Add(int x, int y)
  {
    int iSum = x + y;
    if ((iSum % 5 == 0) && (OnMultipleOfFiveReached != null))
    { OnMultipleOfFiveReached(); }
    return iSum;
  }
}

class Program
{
  static void Main()
  {
    Adder a = new Adder();
    a.OnMultipleOfFiveReached += new Adder.dgEventRaiser(a_MultipleOfFiveReached);
    dgPointer pAdder = new dgPointer(a.Add);

    int iAnswer = pAdder(4, 3);
    Console.WriteLine("iAnswer = {0}", iAnswer);
    iAnswer = pAdder(4, 6);
    Console.WriteLine("iAnswer = {0}", iAnswer);
    Console.ReadKey();
  }

  static void a_MultipleOfFiveReached()
  {
    Console.WriteLine("Multiple of five reached!");
  }
}
In separate class:

class TransactionGeneratedEventArgs : EventArgs
    {
        public Transaction TransactionGenerated { get; set; }

        public TransactionGeneratedEventArgs(Transaction trs)
        {
            TransactionGenerated = trs;
        }
    }
PUBLISHER class:

Declare:
	public delegate void dgEventRaiser();
	public event dgEventRaiser OnMultipleOfFiveReached;
Raise:
	if (OnMultipleOfFiveReached != null)
		{ OnMultipleOfFiveReached(); }
C# 6.0(?):
  OnMultipleOfFiveReached?.Invoke(this, e);

SUBSCRIBER class:

Subscribe:
	Adder a = new Adder();
	a.OnMultipleOfFiveReached += new Adder.dgEventRaiser(a_MultipleOfFiveReached);

Handling the event:
  	static void a_MultipleOfFiveReached()
  	{
  		Console.WriteLine("Multiple of five reached!");
  	}