zoukankan      html  css  js  c++  java
  • C#: Raising an inherited event

    Q:

    A base class that contains the following events:

    public event EventHandler Loading;
    public event EventHandler Finished;
    

    In a class that inherits from this base class I try to raise the event:

    this.Loading(this, new EventHandler()); // All we care about is which object is loading
    

    Received the following error:

    The event 'BaseClass.Loading' can only appear on the left hand side of += or -= (BaseClass')

    I am assuming I cannot access these events the same as other inherited members?

    A1:

    You can only access an event in the declaring class, as .NET creates private instance variables behind the scenes that actually hold the delegate. Doing this..

    public event EventHandler MyPropertyChanged;
    

      

    is actually doing this;

    privateEventHandler myPropertyChangedDelegate;publiceventEventHandlerMyPropertyChanged{
        add { myPropertyChangedDelegate += value;}
        remove { myPropertyChangedDelegate -= value;}}
    

      

    and doing this...

    MyPropertyChanged(this,EventArgs.Empty);
    

      

    is actually this...

    myPropertyChangedDelegate(this,EventArgs.Empty);
    

      

    So you can (obviously) only access the private delegate instance variable from within the declaring class.

    The convention is to provide something like this in the declaring class..

    protected virtual void OnMyPropertyChanged(EventArgs e)
    {
      EventHandler invoker =MyPropertyChanged;
      if(invoker !=null)
        invoker(this, e);
    }

    You can then call OnMyPropertyChanged(EventArgs.Empty) from anywhere in that class or below the inheritance heirarchy to invoke the event.

    A2:

    What you have to do , is this:

    In your base class (where you have declared the events), create protected methods which can be used to raise the events:

    public class MyClass
    {
       public event EventHandler Loading;
       public event EventHandler Finished;
    
       protected virtual void OnLoading(EventArgs e)
       {
           EventHandler handler = Loading;
           if( handler != null )
           {
               handler(this, e);
           }
       }
    
       protected virtual void OnFinished(EventArgs e)
       {
           EventHandler handler = Finished;
           if( handler != null )
           {
               handler(this, e);
           }
       }
    }
    

      

    (Note that you should probably change those methods, in order to check whether you have to Invoke the eventhandler or not).

    Then, in classes that inherit from this base class, you can just call the OnFinished or OnLoading methods to raise the events:

    public AnotherClass : MyClass
    {
        public void DoSomeStuff()
        {
            ...
            OnLoading(EventArgs.Empty);
            ...
            OnFinished(EventArgs.Empty);
        }
    }
    
    from:http://stackoverflow.com/questions/756237/c-raising-an-inherited-event

      

  • 相关阅读:
    Java查找指定文件中指定字符的个数
    推荐系统(CTR领域)实战入门指南
    xgboost 实践
    pandas 获取列名
    pandas 标签映射成数值的几种方法
    pandas 删除列
    pandas 聚合求和等操作
    dataframe检查重复值,去重
    linux 解压缩文件(tar和zip)
    kaggle——Bag of Words Meets Bags of Popcorn(IMDB电影评论情感分类实践)
  • 原文地址:https://www.cnblogs.com/SFAN/p/2985014.html
Copyright © 2011-2022 走看看