USE OF MULTICAST DELEGATE IN C#?

Anupam Mishra

Total Post:135

Points:949
Posted by  Anupam Mishra
C#  .NET 
C#
 527  View(s)
Ratings:
Rate this:
Hi Everyone
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?

Thakyou
  1. Anupam Mishra

    Post:135

    Points:949
    Re: Use of Multicast Delegate in C#?

    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
     public delegate void Notification(string msg);
        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 = new Deligatedemo();
        Deligatedemo. Notification notified = null;
        notified+= new DeligateClass. Notification (AlertMsg);
        notified+= new      DeligateClass. Notification (WarningMsg);
        dc.Process(notified);
     }

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!