using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp13
{
class Program
{
public class Subscriber
{
public static void Main()
{
Publisher publisher = new Publisher();
publisher.BeginAdd += AddCallback;
publisher.EndAdd += EndCallBack;
Console.WriteLine(publisher.Multiply(2.3f, 4.5f));
publisher.BeginAdd -= AddCallback;
publisher.EndAdd -= EndCallBack;
Console.WriteLine(publisher.Multiply(3.3f, 4.4f));
Console.ReadLine();
}
public static void AddCallback(string message)
{
Console.WriteLine("Callback - " + message);
}
public static void EndCallBack(string message)
{
Console.WriteLine("Callback - " + message);
}
}
public class Publisher
{
public delegate void Notify(string message); // Declare delegate.
public event Notify BeginAdd; // Declare event.
public event Notify EndAdd;
public float Multiply(float a, float b)
{
OnBeginAdd(); // Raise event.
OnEndAdd();
return a * b;
}
private void OnBeginAdd()
{
if (BeginAdd != null)
BeginAdd("Starting multiplication!"); // Call callback method.
}
private void OnEndAdd()
{
if (EndAdd != null)
EndAdd("Completing multiplication!");
}
}
}
}
How do I correct the syntax for adding OnEndAdd(); into the Multiply function so that it only does the call back after the completion of the function? I've tried adding it after the return statement but that obviously doesn't work, can't seem to figure out another way...