Question

Please tell me what's the different between Class1 and Class2 or the different between Event and Delegate ? I test my class in Form1 and have the same result

//Class1.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

class Class1EventArgs
{
    public StringBuilder CallStack;

    public Class1EventArgs() {
        CallStack = new StringBuilder();
    }
}

delegate void Class1EventHandler(Object sender, Class1EventArgs e);

class Class1
{
    public event Class1EventHandler EventDelegate;

    public void EventCaller()
    {
        Class1EventArgs e = new Class1EventArgs();
        e.CallStack.AppendLine("EventCaller");
        OnClass1Event(e);
    }

    protected virtual void OnClass1Event(Class1EventArgs e)
    {
        if (EventDelegate != null)            
        {               
            EventDelegate(this, e);
            System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
        }
    }

}

//Class2.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

public class Class2EventArgs
{
    public StringBuilder CallStack = new StringBuilder();
}

public delegate void Class2EventHandler(object sender,Class2EventArgs e);

class Class2
{
    public Class2EventHandler EventDelegate;

    public Class2()
    {
        EventDelegate = new Class2EventHandler(this.OnEventHappen);
    }

    public void EventCaller()
    {
        Class2EventArgs e = new Class2EventArgs();

        e.CallStack.AppendLine("EventCaller");

        EventDelegate.Invoke(this, e);

        System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
    }

    protected virtual void OnEventHappen(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("OnEventHappen");
    }

}

//Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

public partial class Form1 : Form
{
    Class1 c1 = new Class1();
    Class2 c2 = new Class2();

    public Form1()
    {
        InitializeComponent();
    }

    #region "Button1"      

    private void button1_Click(object sender, EventArgs e)
    {
        c1.EventCaller();
    }

    private void c1_EvenDelegate(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate");
    }

    private void c1_EvenDelegate_2(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate_2");
    }

    #endregion

    private void Form1_Load(object sender, EventArgs e)
    {
        //c1 = new Class1();
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate);
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate_2);

        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen1);
        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen2);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        c2.EventCaller();
    }

    private void c2_OnEventHappen1(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen1");
    }

    private void c2_OnEventHappen2(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen2");
    }
}
Was it helpful?

Solution

Event is a special system stack-like variable which holds pointer(s) to functions. An event can hold a only special type of functions as its defined by a delegate. Delegate is type for a function - it's like a definition of what a function signature must look like: this function must have 2 int parameters. This is what basically a delegate means to the system. When you instantiate a delegate with a function which matches its description the instance of the delegate now holds address of that function. Is this clear somewhat or not yet ... ?:) This is how I see it.

So basically an event holds a pointer to pre-defined void type of function(the delegate type).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top