protected virtual void OnUnaryCalculation(ICalculation calculation) { CalculationComplete?.Invoke(this, new CalcEventArgs() { Calculation = calculation }); }
// This method is called with delegates for // both the collection of calculate methods and the callback method. public void CallCalculator(CalculateMethod cm, CalculationComplete cc) { // TODO: Call the multicast CalculateMethod delegate. cm(); // TODO: Call the callback method. cc(); }
// This method is called with delegates for // both the calculate method and the callback method. public void CallCalculator(CalculateMethod cm, CalculationComplete cc) { // Get the input value. int inputInt = GetNumericValue(); // Call the calculate method. // TODO: Call the CalculateMethod delegate to call the // calculate method that is stored in it passing in the // value in the variable inputInt. // Call the callback method. // TODO: Call the CalculationComplete delegate to call // the callback method that is stored in it. }
// This method is called with delegates for // both the collection of calculate methods and the callback method. public void CallCalculator(CalculateMethod cm, CalculationComplete cc) { Console.WriteLine("Starting total is: {0}", TotalValues); // TODO: Code a foreach loop to loop through the multicast // CalculateMethod delegate to execute one method at a // time to capture the return value, which is the name of // the calculate method that was called: // Include the commented block of code below in the loop. // Uncomment the block after inserting it into the loop. // Give the reference name "m" to each instance of the // delegate in the foreach loop. //Console.WriteLine("\nResult after {0}ing {1} is {2}.", // m(), values[0], TotalValues); //values.RemoveAt(0); // Call the callback method. cc(); }
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(); }
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". 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 delegate method to call Add method. // TODO: Load the already declared CalculateMethod delegate (name is cm) // with the Add method in the Calculator class. cm = new CalculateMethod(c.Add); // 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. cm = new CalculateMethod(c.Subtract); // 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(); }