I am using delegates in program. But situation is to more than one method to adding to a delegate. Please anyone can give me a solution with example how to done this?
Multicast delegate is an extension of normal delegate. It helps you to point more than one method at a single moment of time.
In a windows application, we created a button on a form and on the button click, we want to invoke two methods (AlertMsg(), WarningMsg()). So the code is as follow:
public class Deligatedemo { public Deligatedemo () { } // Declare a delegate publicdelegatevoidNotification(stringmsg); public void Process(Notification handler) { if (handler != null) handler("Alert Message"); if (handler != null) handler("Warning Message"); } } public class demoClass { //This method will show alert message to user public static void AlertMsg (string s) { MessageBox.Show(s); }
//This method will show warning message to user public static void WarningMsg(string s) { MessageBox.Show(s); } } private void button1_Click(object sender, System.EventArgs e) { Deligatedemo dc =newDeligatedemo(); Deligatedemo. Notification notified =null; notified+=newDeligateClass. Notification (AlertMsg); notified+=newDeligateClass. Notification (WarningMsg); dc.Process(notified); }
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Multicast delegate is an extension of normal delegate. It helps you to point more than one method at a single moment of time.
In a windows application, we created a button on a form and on the button click, we want to invoke two methods (AlertMsg(), WarningMsg()). So the code is as follow: