Example #1
0
        static void Main()
        {
            string option = "";

            // TODO:  Take note that the CalculateMethod delegate is being
            //      declared here.  It will be loaded in the TODOs below.  Its
            //      reference name is "cm".
            CalculateMethod cm;

            Program p = new Program();
            Calculator c = new Calculator();

            // Declare an anonymous method.
            DisplayMenu menu = delegate
            {
                Console.WriteLine("\nCalculator for Add and Subtract");
                Console.WriteLine("\n\t1. Add");
                Console.WriteLine("\t2. Subtract");
                Console.WriteLine("\tX. Exit Calculator");
                Console.Write("\nEnter option: ");
            };

            // TODO: Declare the CalculationComplete delegate and load it with
            //      the CalculationCompleteCallback method. Give the reference
            //      name of the delegate "cc".

            while (option.ToUpper() != "X")
            {
                // Call the anonymous method.
                menu();

                // Get the option from the user.
                option = Console.ReadLine();
                Console.WriteLine();

                switch (option.ToUpper())
                {
                    case "1":

                        // Load delegate method to call Add method.
                        // TODO: Load the already declared CalculateMethod delegate (name is cm)
                        //      with the Add method in the Calculator class.

                        // TODO:  Take note that the CallCalculator method in the Calculator class is being
                        //      called here to pass in the two loaded delegates for the calculate
                        //      method and the callback method.
                        c.CallCalculator(cm, cc);

                        break;

                    case "2":

                        // Load delegate method to call Subtract method.
                        // TODO: Load the already declared CalculateMethod delegate (name is cm)
                        //      with the Subtract method in the Calculator class.

                        // TODO:  Take note that the CallCalculator method in the Calculator class is being
                        //      called here to pass in the two loaded delegates for the calculate
                        //      method and the callback method.
                        c.CallCalculator(cm, cc);

                        break;

                    case "X":
                        break;

                    default:
                        Console.WriteLine("Menu option {0} is not valid.",
                            option);
                        break;
                }
            }

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValues);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
Example #2
0
        static void Main()
        {
            string option = "";

            CalculateMethod cm = null;     // Delegate for calculation methods.

            Program p = new Program();
            Calculator c = new Calculator();

            // Declare an anonymous method.
            DisplayMenu menu = delegate
            {
                Console.WriteLine("\nCalculator for Add and Subtract");
                Console.WriteLine("\n\t1. Add");
                Console.WriteLine("\t2. Subtract");
                Console.WriteLine("\tX. Exit Calculator");
                Console.Write("\nEnter option: ");
            };

            // Declare the CalculationComplete delegate and load it with
            //      the CalculationCompleteCallback method.
            CalculationComplete cc = new CalculationComplete(p.CalculationCompleteCallback);

            while (option.ToUpper() != "X")
            {
                // Call the anonymous method.
                menu();

                // Get the option from the user.
                option = Console.ReadLine();
                Console.WriteLine();

                switch (option.ToUpper())
                {
                    case "1":

                        // Load multicast delegate to call Add method.
                        // TODO: Include the Add method in the multicast
                        //      CalculateMethod delegate.

                        // Loads a numeric value into a collection to be used
                        //       by the method when it is executed.
                        c.values.Add(c.GetNumericValue());

                        break;

                    case "2":

                        // Load delegate to call Subtract method.
                        // TODO: Include the Subtract method in the multicast
                        //      CalculateMethod delegate.

                        // Loads a numeric value into a collection to be used
                        //       by the method when it is executed.
                        c.values.Add(c.GetNumericValue());

                        break;

                    case "X":
                        break;

                    default:
                        Console.WriteLine("Menu option {0} is not valid.",
                            option);
                        break;
                }
            }

            // Call the method in the Calculator class
            //      to pass in the two loaded delegates for the calculate
            //      methods and the callback method.  Do this ONLY if
            //      the multicast CalculateMethod delegate has any calculate methods
            //      loaded into it.
            if (cm == null)
            {
                Console.WriteLine("\nNo calculations were requested.");
            }
            else
            {
                c.CallCalculator(cm, cc);
            }

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValues);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }