Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多播委托和事件_C#_Oop_Events_Delegates - Fatal编程技术网

C# 多播委托和事件

C# 多播委托和事件,c#,oop,events,delegates,C#,Oop,Events,Delegates,我正在研究关于代表的问题。几天前,我为一个多播代理做了一个示例,并在这里进行了回顾,清楚地了解了多播代理 但是现在我尝试用一个事件来做一个多播委托示例。但我在做样品的时候有些怀疑。在上面的链接中,我在一个类中完成了所有函数和委托声明,并使用+=将函数添加到委托中,然后只调用委托。这样就调用了委托中的所有函数 但是现在我在两个不同的类中做这件事,并试图在一个事件的帮助下完成所有的函数。我在下面提供我的当前代码 public partial class Form1 : Form { publ

我正在研究关于代表的问题。几天前,我为一个多播代理做了一个示例,并在这里进行了回顾,清楚地了解了多播代理

但是现在我尝试用一个事件来做一个多播委托示例。但我在做样品的时候有些怀疑。在上面的链接中,我在一个类中完成了所有函数和委托声明,并使用+=将函数添加到委托中,然后只调用委托。这样就调用了委托中的所有函数

但是现在我在两个不同的类中做这件事,并试图在一个事件的帮助下完成所有的函数。我在下面提供我的当前代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ArithmeticOperations aOperations = new ArithmeticOperations();
        aOperations.StartCalculations += new ArithmeticOperations.BasicCalculations(aOperations_StartCalculations);
        aOperations.PerformCalculations(20, 10);
    }

    void aOperations_StartCalculations(string msg)
    {
        MessageBox.Show(msg);
    }
}


class ArithmeticOperations
{
    public delegate void BasicCalculations(string msg);

    public event BasicCalculations StartCalculations;


    public void PerformCalculations(int n1, int n2)
    {
        StartCalculations("Operation Success");
    }

    void Add(int num1, int num2)
    {
        MessageBox.Show("Performing addition.");
    }

    void Sub(int num1, int num2)
    {
        MessageBox.Show("Performing substraction.");
    }

    void Mul(int num1, int num2)
    {
        MessageBox.Show("Performing multiplication.");
    }

    void Div(int num1, int num2)
    {
        MessageBox.Show("Performing division.");
    }

}
在这里的表格1是我的主要类和算术运算类是用来做功能。在这份声明上什么时候

aOperations.PerformCalculations(20, 10);
在表1中,将执行算术运算类中的PerformCalculation()函数


但我的疑问是,我如何将所有Add、Sub、Mul和Div函数注册到算术运算类中的委托,以便通过调用委托对象调用所有函数并返回“Operation Success”对于Form1类中的事件回调函数?

由于
基本计算
采用类型为
string
的单个参数,因此不能使用它直接调用方法
Add
Subtract

如果希望
PerformCalculations
通过多播调用每个方法,则需要一个类型相当于
Action
的委托,例如:

class ArithmeticOperations
{
    public delegate void BasicCalculations(string msg);

    public event BasicCalculations StartCalculations;

    private Action<int, int> calculations;

    public ArithmeticOperations()
    {
        this.calculations += this.Add;
        this.calculations += this.Sub;
    }

    public void PerformCalculations(int n1, int n2)
    {
        this.calculations.Invoke(n1, n2);
        StartCalculations("Operation Success");
    }

    // ...
}
引发事件时,应首先测试是否存在订阅服务器(以避免出现
NullReferenceException
)。方法是获取订阅的任何处理程序,测试
null
,然后调用该处理程序。如果有人在测试
null
事件后取消订阅,这将避免出现
null引用异常:

void Add(int num1, int num2)
{
    var handler = this.StartCalculations;
    if (handler != null)
    {
        handler("Performing addition.");
    }
}
由于每个方法都需要大量代码重复,因此可以将其重构为单独的方法:

void Add(int num1, int num2)
{
    this.OnStartCalculation("Performing addition.");
}

void OnStartCalculation(string message)
{
    var handler = this.StartCalculations;
    if (handler != null)
    {
        handler(message);
    }
}

其中一个选项是创建一个私人事件

  public delegate void BasicCalculations(string msg);

public delegate void DoMath(int na, int nb);
class ArithmeticOperations
{
    public ArithmeticOperations()
    {
        StartMath += new DoMath(ArithmeticOperations_StartMath);

    }

    void ArithmeticOperations_StartMath(int na, int nb)
    {
        Add(na, nb);
        Sub(na, nb);
        Mul(na, nb);
        Div(na,nb);
    }




    public event BasicCalculations StartCalculations;
    private event DoMath StartMath;

    public void PerformCalculations(int n1, int n2)
    {
        StartMath(n1, n2);
        StartCalculations("Operation Success");
    }

    void Add(int num1, int num2)
    {
        Console.WriteLine("Performing addition.");
    }

    void Sub(int num1, int num2)
    {
         Console.WriteLine("Performing substraction.");
    }

    void Mul(int num1, int num2)
    {
         Console.WriteLine("Performing multiplication.");
    }

    void Div(int num1, int num2)
    {
         Console.WriteLine("Performing division.");
    }

}

您的委托类型
BasicCalculations
(嵌套在类中,为什么?)。您的
Add
方法等接受两个参数,这两个参数都是
int
。那么这些方法怎么能直接适用于你的活动呢?哎呀…我没看到…你说的对…谢谢Hanks Ergwun。现在我明白了。我第一次听说这件事。谢谢你提供的更多信息。谢谢你。你的建议也有助于我更好地理解它。谢谢
  public delegate void BasicCalculations(string msg);

public delegate void DoMath(int na, int nb);
class ArithmeticOperations
{
    public ArithmeticOperations()
    {
        StartMath += new DoMath(ArithmeticOperations_StartMath);

    }

    void ArithmeticOperations_StartMath(int na, int nb)
    {
        Add(na, nb);
        Sub(na, nb);
        Mul(na, nb);
        Div(na,nb);
    }




    public event BasicCalculations StartCalculations;
    private event DoMath StartMath;

    public void PerformCalculations(int n1, int n2)
    {
        StartMath(n1, n2);
        StartCalculations("Operation Success");
    }

    void Add(int num1, int num2)
    {
        Console.WriteLine("Performing addition.");
    }

    void Sub(int num1, int num2)
    {
         Console.WriteLine("Performing substraction.");
    }

    void Mul(int num1, int num2)
    {
         Console.WriteLine("Performing multiplication.");
    }

    void Div(int num1, int num2)
    {
         Console.WriteLine("Performing division.");
    }

}