Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to understand delegates and events and wrote a simple app based on an example.

C#
namespace DelegateTest
{
    public partial class Form1 : Form
    {
        
            BroadcastClass B = new BroadcastClass();
            ListenToClass1 L1 = new ListenToClass1();
            ListenToCalss2 L2 = new ListenToCalss2();
            ListenToClass3 L3 = new ListenToClass3();
        public Form1()
        {
            InitializeComponent();
           // wire methods of one class to the event of a single class
            B.EventName += new DelegateName(L1.IHeardSomething);
            B.EventName += new DelegateName(L2.IHeardSomething);
            B.EventName += new DelegateName(L3.IHeardSomething);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            B.Start();
        }
    }
}


I then modified how i passed in the methods to the event (see below) and it still works. So i'm wondering why the the example is different and why my way works and is my way ok or is it wrong in some way. All examples use the above method.

C#
namespace DelegateTest
{
    public partial class Form1 : Form
    {
        
            BroadcastClass B = new BroadcastClass();
            ListenToClass1 L1 = new ListenToClass1();
            ListenToCalss2 L2 = new ListenToCalss2();
            ListenToClass3 L3 = new ListenToClass3();
        public Form1()
        {
            InitializeComponent();
           
            B.EventName += L1.IHeardSomething;
            B.EventName += L2.IHeardSomething;
            B.EventName += L3.IHeardSomething;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            B.Start();
        }
    }
}

regards
Posted
Updated 4-Jul-12 20:53pm
v2
Comments
Prasad_Kulkarni 5-Jul-12 2:54am    
..and your question is??

1 solution

I don't know where you found your example but Microsoft provide a very good one here[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900