开发者

Is it possible to create an event in an abstract class?

开发者 https://www.devze.com 2023-03-21 07:38 出处:网络
I have abstract class public abstract class BaseClass { public delegate void ContextMenuClickHandler(object sender, Point point);

I have abstract class

public abstract class BaseClass
{
    public delegate void ContextMenuClickHandler(object sender, Point point);
    public event ContextMenuClickHandler OnContextMenuClick;

    public void OnMouseClick(MouseEventArgs e)
    {
        OnContextMenuClick(this, new Point((int) x, (int) y));
    }
}

and I plug event:

Base.ContextMenuClickHa开发者_开发百科ndler += ShowContextMenu;//error

void ShowContextMenu(object sender, Point point)
{

}

error:

'Base.ContextMenuClickHandler' is a 'type', which is not valid in the given context

How to fix it?


The problem here is you are using the event type, ContextMenuClickHandler, instead of the event name, OnContextMenuClick. Try the following

base.OnContextMenuClick += ShowContextMenu


OnContextMenuClick += ShowContextMenu;//fix

you used the wrong variable. OnContextMenuClick is your event that you subscribe to.

but in your case, why do not use a virtual method?


There is an example here, have a look: http://msdn.microsoft.com/en-us/library/hy3sefw3.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号