Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

event Delegates callback to another event delegates?

Status
Not open for further replies.

moMoney2009

Programmer
Nov 7, 2007
7
US
Is that possible. More specifically, can I create Delegate A which then is assigned to callback to Delegate B, BUT then also have Delegate B callback to a particular method?

keep in mind that I mean event delegates.

code example:
Code:
public class classA
{
   public event EventHandler Click;
}
Code:
public class classB
{
   public event EventHandler classA_Click;
}
Code:
public class classC
{
   public classC(classA a, classB b)
   {
      a.Click += b.classA_Click;
      b.classA_Click += myMethod;
   }
   
   private void myMethod(object sender, EventArgs e)
   {
      System.Windows.Forms.MessageBox("callback successful");
   }
}

keep in mind with the "off the top" example i just wrote someone may be tempted to point out that what I am asking would be pointless since I could just assign the following

Code:
a.Click += myMethod;

I would rather someone let that response slide and just know if what I proposed can work ... AND if not the way I coded it, then how do I properly do so?

Thanks :)
 
well i figured out the answer to my own question so here is the answer for anyone who is interested. There is a gap in my answer ... the WHY ... but I do know the solution.

For whatever (the WHY) reason, the other code that I posted will not work. Delegates as far as I can see will not delegate directly to another delegate by simply using the += operation. Although, I will not rule out the possiblity that they CAN delegate directly (I just don't know how). But instead you can have them delegate indirectly using their feature of notification.

I don't like the solution of indirect delegation (since a method call is the most expensive type of memory operation) and considering a situation where you might delegate a line of up to twenty delegates ... (I don't know a real-world application of that possibility but nonetheless it is possible) it would be very inefficient

Code:
using System.Windows.Forms;
public class ClassA
{
   public event EventHandler SearchClick
   private Button searchBtn;
   public ClassA()
   {
      searchBtn = new Button("Search");
      searchBtn.Click += new EventHandler(indirectMethod);
   }
   private void indirectMethod(object sender, EventArgs e)
   {
      /*this method does nothing more than insure that 
        SeachClick is NOT null and then use its notification
        ability*/

      if ( SearchClick != null )
         SearchClick(sender, e);
   }
}

now for the class that wants to listen for searchBtn to fire a Click event

Code:
using System.Windows.Forms;
public class Listener
{
   public Listener(ClassA a)
   {
      a.SearchClick += new EventHandler(ClassA_searchBtn_Click);
   }
   private void ClassA_searchBtn_Click(object sender, EventArgs e)
   {
      MessageBox.Show("Yay! The callback worked!");
   }

}


In the end, this solution is nothing special, just a style since you could do this exact same thing with a different approach. But I hate dealing with Properties that are unnecessary.

Code:
public class ClassA
{
   private Button searchBtn;

   public ClassA()
   {
      searchBtn = new Button();
   }

   public Button SearchBtn
   {
      get { return(searchBtn); }
   }
   
   //or if you don't like Properties
   public Button getSearchBtn()
   {
      return(searchBtn);
   }

   //you could also do a modifier rather than an accesser
   public void addSearchBtnListener(EventHandler del)
   {
      if (del != null)
         searchBtn.Click += del;
   }
}

Code:
public class Listener
{
   public Listener(ClassA a)
   {
      //approach 1
      a.SearchBtn.Click += new EventHandler(myMethod);
     
      //approach 2
      a.getSearchBtn().Click += new EventHandler(myMethod);
 
      //approach 3
      a.addSearchBtnListener(new EventHandler(myMethod));
   }

   private void myMethod(object sender, EventArgs e)
   {
      MessageBox.Show("Yay!");
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top