【问题标题】:Subscribing to event of multiple instances of a class订阅一个类的多个实例的事件
【发布时间】:2013-01-28 05:05:35
【问题描述】:

我有一个自定义 Shape 类的程序,如下所示

class Shape
{
public:
Event<ShapeArgs> shapeBurst;
virtual void onMousePress(); //inside this, shapeBurst event is notifed
};

我的程序中有 15 个 Shape 类的实例。我必须为所有这些实例订阅事件并提供一个通用处理程序。但是,我对如何订阅所有 15 个类的事件有疑问。
我应该在下面的表单中写下每个实例订阅 shapeBurst 方法还是有更好的方法?

shape.shapeBurst += new ShapeBurstEventHandler(ShapeBurstMethod);

【问题讨论】:

    标签: c# algorithm events


    【解决方案1】:

    如果你有 15 个形状对象,那么你可以使用对象数组而不是 15 个变量,并使用循环来订阅事件。

    for(int i=0; i < shape.Length;i++)
      shape[i].shapeBurst += new ShapeBurstEventHandler(ShapeBurstMethod);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多