Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up Delegates between my classes?

I'm working with a bit of code that is structured thusly:

Form A has user controls B and G within it. User control B has User control D within it, and user control D has a "refresh" method. User control G has user control F within it, and user control F needs to call the "refresh" method in D.

1) Short of restructuring the code (It's legacy code, so it's off the table as an option), are delegates the best way to go about handling this issue? If not, do you have another suggestion?

2) I have no experience with delegate functions -- is there a good primer or example that I could use to adapt to my code to achieve the desired functionality?

like image 421
Raven Dreamer Avatar asked Jul 14 '10 13:07

Raven Dreamer


1 Answers

First, F should not know anything about D and its refresh function. Add event to F, which is raised when necessary. If form A knows about F, subsctibe to this event from A. If not, make similar event in G, and subscribe to it in A. In A event handler, call Refresh directly, or call B method, which calls D.refresh.

It looks like my answer is even less readable than your question :) It should look like this:

F raises event -> G handles F's event and raises event -> A handles G's event and calls B method -> B method calls D.refresh

like image 194
Alex F Avatar answered Nov 01 '22 02:11

Alex F