Case Study – Event Practice in C#

In C# practice, we always use Event as an observer for property modification. In this post, I am sharing a practice case regarding piggy bank monitoring.


Background

A piggy bank can help us with saving an amount of money that we planned. Every time, we deposit gold coins into the piggy bank, the balance will increase accordingly.

In this case, we set our goal about is $500. We expect the application to tell us how much is the total amount in the piggy bank after each deposit and inform us when we reach our goal.


Approach Code

namespace EventsSolution
{
    public delegate void BalanceEventHandler(decimal theValue);

    class PiggyBank
    {
        private decimal m_bankBalance;
        public event BalanceEventHandler balanceChanged;

        public decimal theBalance {
            set {
                m_bankBalance = value;
                balanceChanged(value);
            }
            get {
                return m_bankBalance;
            }
        }
    }

    class BalanceLogger
    {
        public void balanceLog(decimal amount) {
            Console.WriteLine("The balance amount is {0}", amount);
        }
    }

    class BalanceWatcher
    {
        public void balanceWatch(decimal amount) {
            if (amount > 500.0m)
                Console.WriteLine("You reached your savings goal! You have {0}", amount);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            PiggyBank pb = new PiggyBank();
            BalanceLogger bl = new BalanceLogger();
            BalanceWatcher bw = new BalanceWatcher();

            pb.balanceChanged += bl.balanceLog;
            pb.balanceChanged += bw.balanceWatch;

            string theStr;
            do {
                Console.WriteLine("How much to deposit?");

                theStr = Console.ReadLine();
                if (!theStr.Equals("exit")) {
                    decimal newVal = decimal.Parse(theStr);

                    pb.theBalance += newVal;
                }
            } while (!theStr.Equals("exit"));
        }
    }
}

We declare a delegate at the beginning of the project by the following code. The delegate does not have a return type and it takes a decimal parameter.

public delegate void BalanceEventHandler(decimal theValue);

We declare an Event property in the PiggyBank class.

class PiggyBank
    {
        private decimal m_bankBalance;
        //Declare an event
        public event BalanceEventHandler balanceChanged;

        public decimal theBalance {
            set {
                m_bankBalance = value;
                //Setup an Event observer, every time theBalance amended, the event is triggerd.
                balanceChanged(value);
            }
            get {
                return m_bankBalance;
            }
        }
    }

We switch on the Event mechanism after we initialised the PiggyBank as followed:

pb.balanceChanged += bl.balanceLog;
pb.balanceChanged += bw.balanceWatch;

Leave a Reply

Your email address will not be published. Required fields are marked *