Delegate

The following code illustrates a situation where a boss wants to keep track of the work progress of its subordinates:

using System;

namespace BigCompany {
public class Boss
{
public void WorkerPercentageDone(int percentage)
{
Console.WriteLine("I am the boss, and my worker is " + percentage + "% done.");
}
}

public class Worker
{
Boss boss = null;

public Worker(Boss boss)
{
this.boss = boss;
}

public void DoWork()
{
boss.WorkerPercentageDone(20);
boss.WorkerPercentageDone(50);
boss.WorkerPercentageDone(80);
boss.WorkerPercentageDone(100);
}
}

class Program
{
static void Main(string[] args)
{
Boss theBoss = new Boss();
Worker someWorker = new Worker(theBoss);
someWorker.DoWork();
}
}
}
By using an event delegates, change the code above so that another object of type "Manager", that you will write, will also get notified by the worker's progress.

Questions by musclebai   answers by musclebai

Showing Answers 1 - 6 of 6 Answers

using System;

namespace BigCompany
{
    public class Boss : System.EventArgs
    {
        public delegate void WorkerUpdateDelegate(int progress);
        public
event WorkerUpdateDelegate WorkerUpdateEvent;

        public void WorkerPercentageDone(int percentage)
        {
            Console.WriteLine("I am the boss, and my worker is " + percentage + "% done.");

            if (WorkerUpdateEvent != null)
            {
                WorkerUpdateEvent(percentage);
            }

        }
    }

    public class Worker
    {
        Boss boss = null;

        public Worker(Boss boss)
        {
            this.boss = boss;
        }

        public void DoWork()
        {
            boss.WorkerPercentageDone(20);
            boss.WorkerPercentageDone(50);
            boss.WorkerPercentageDone(80);
            boss.WorkerPercentageDone(100);
        }
    }

    public class Manager
    {
        public void OnManagerUpdate(int percentage)
        {
            Console.WriteLine("I am the Manager, and my worker is " + percentage + "% done.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Boss theBoss = new Boss();
            Manager theMgr = new Manager();
            theBoss.WorkerUpdateEvent += theMgr.OnManagerUpdate;

            Worker someWorker = new Worker(theBoss);
            someWorker.DoWork();
        }
    }
}

ecklerpa

  • Jun 16th, 2009
 

public class Boss
        {
            public void WorkerPercentageDone(int percentage)
            {
                Console.WriteLine("I am the boss, and my worker is " + percentage + "% done.");
            }
        }

        public class Manager
        {
            public void OnManagerUpdate(int percentage)
            {
                Console.WriteLine("I am the Manager, and my worker is " + percentage + "% done.");
            }
        }

        public class Worker
        {
            Boss boss = null;
            public delegate void WorkerUpdateDelegate(int progress);
            public event WorkerUpdateDelegate WorkerUpdateEvent;

            public Worker(Boss boss)
            {
                this.boss = boss;
            }

            public void DoWork()
            {
                for (int x = 20; x <= 100; x += 20)
                {
                    boss.WorkerPercentageDone(x);
                    if (WorkerUpdateEvent != null)
                    {
                        WorkerUpdateEvent(x);
                    }
                }
            }
     }

Main()
{
      Boss theBoss = new Boss();
      Manager theMgr = new Manager();

      Worker someWorker = new Worker(theBoss);
      someWorker.WorkerUpdateEvent += theMgr.OnManagerUpdate;
      someWorker.DoWork();
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions