How to configure delegates between my classes?

I work with a little code that is structured this way:

Form A has user controls B and G. User control B has user control D inside it, and user control D has an update method. User control G has user control F inside it, and user control F needs to call the refresh method in D.

1) With the exception of code restructuring (this is legacy code, so it can be disabled from the table as an option), are delegates the best way to solve this problem? If not, do you have another suggestion?

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

+6
c # winforms delegates
source share
2 answers

First, F does not need to know anything about D and its update function. Add an event to F that occurs when necessary. If form A knows about F, subsctibe to this event from A. If not, make a similar event in G and subscribe to it in A. In the event handler, call Refresh directly or call method B, which calls D.refresh.

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

F triggers an event → G processes an event F and triggers an event → A processes a G-event and calls method B → B-method D.refresh

+4
source share

For delegates see this: http://www.akadia.com/services/dotnet_delegates_and_events.html

For events, you must ensure that the material runs in the correct order. In the current scenario, I assume that a good way would be to expose an event from G that should fire when something in F changes. And we will have a handler that subscribes to this event in B, which will eventually trigger an update. Several more events and delegates will be involved to transfer calls from parent to child and vice versa. Keep in mind the concept of a mechanical device: the part that STARTS the movement is the one who triggers (triggers the event) others. When others move, they cause other related parts ... and ultimately something moves far. Hope this helps.

0
source share

All Articles