事件问题 [英] Event question

查看:57
本文介绍了事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基类中的
我已经声明了以下事件处理程序:

公共抽象类MyBaseClass :: MarshalByRefObject

{

.....

公共活动EventHandler OnMsgSent;

....

}

在派生类中,我尝试按以下方式调用此事件:


public override void MsgSent()

{

if(OnMsgSent!= null)

{

GbMessageEventArgs e = new GbMessageEventArgs(this);

e.InfoString ="消息已成功发送!;

OnMsgSent(this,e);

}

}


但是当我编译这个时,我收到以下错误消息:

"事件,< my namespace>。< base class> ; .OnMsgSent''只能出现在

左手边的+ = ...."


我做错了什么?


感谢提示。

Rainer

Hi,

in a base class I have declared the following event handler:

public abstract class MyBaseClass : :MarshalByRefObject
{
.....
public event EventHandler OnMsgSent;
....
}
In a derived class I try to call this event in the following way:

public override void MsgSent()
{
if(OnMsgSent != null)
{
GbMessageEventArgs e = new GbMessageEventArgs(this);
e.InfoString = "Message was successfully sent!";
OnMsgSent(this,e);
}
}

but when I trie to compile this, I get the following error message:
"The event ,<my namespace>.<base class>.OnMsgSent'' can only appear on the
left hand side of += ...."

What is it I am doing wrong?

Thanks for hints.
Rainer

解决方案

我不相信你可以覆盖一个事件。由于OnMsgSent是

定义为偶数使用:


公共事件EventHandler OnMsgSent;


它必须始终是以这种方式定义。你不能创建一个名为OnMsgSent的*方法*

。方法和事件是两回事。


你想要完成什么?


最好的问候

Johann Blake

I don''t believe that you can override an event. Since OnMsgSent is
defined as an even using:

public event EventHandler OnMsgSent;

it must always be defined in this manner. You cannot create a *method*
called OnMsgSent. A method and an event are two different things.

What exactly do you want to accomplish?

Best Regards
Johann Blake


嗨约翰,


感谢您的回答!


我(在leas我不知道......)不要覆盖事件。

在基类(这是抽象的)中我声明了抽象方法:

public abstract void MsgSent();

我的意图是每个派生类都必须实现这个mehtod。

这个方法的目的是调用所有分配的代表 - OnMsgSent -

声明为事件句柄,如前所示。


问候

Rainer

" Johann Blake" <乔********* @ yahoo.com> schrieb im Newsbeitrag

新闻:11 ******************** @ g43g2000cwa.googlegrou ps.com ...
Hi Johann,

thanks for answering!

I (at leas I am not aware of...) don''t override the event.
In the base class (which is abstract) I declared the abstract method:
public abstract void MsgSent();
My intention is that every derived class must implement this mehtod.
The purpose of this method is to call all assigned delegates - OnMsgSent -
declared as event handles as previously shown.

Regards
Rainer

"Johann Blake" <jo*********@yahoo.com> schrieb im Newsbeitrag
news:11********************@g43g2000cwa.googlegrou ps.com...
我不相信你可以覆盖一个事件。由于OnMsgSent被定义为偶数使用:

公共事件EventHandler OnMsgSent;

必须始终以这种方式定义它。你不能创建一个名为OnMsgSent的*方法*
。方法和事件是两回事。

你到底想要完成什么?

最好的问候
Johann Blake
I don''t believe that you can override an event. Since OnMsgSent is
defined as an even using:

public event EventHandler OnMsgSent;

it must always be defined in this manner. You cannot create a *method*
called OnMsgSent. A method and an event are two different things.

What exactly do you want to accomplish?

Best Regards
Johann Blake



Rainer Queck写道:
Rainer Queck wrote:


在基类中我声明了以下事件处理程序:
<公共抽象类MyBaseClass :: MarshalByRefObject
{
....
公共事件EventHandler OnMsgSent;
...
}

在派生类中,我尝试按以下方式调用此事件:

public override void MsgSent()
{
if(OnMsgSent!= null)
{
GbMessageEventArgs e = new GbMessageEventArgs(this);
e.InfoString ="消息已成功发送!" ;;
OnMsgSent(this,e);
} <但是,当我编译这个时,我收到以下错误消息:
"事件,< my namespace>。< base class> .OnMsgSent' ' 能够 只出现在
左侧+ = ...."


[我怀疑这个错误出现在你注册事件的地方,可以

你告诉我们你如何注册活动]。

我做错了什么?

感谢提示。
Rainer
Hi,

in a base class I have declared the following event handler:

public abstract class MyBaseClass : :MarshalByRefObject
{
....
public event EventHandler OnMsgSent;
...
}
In a derived class I try to call this event in the following way:

public override void MsgSent()
{
if(OnMsgSent != null)
{
GbMessageEventArgs e = new GbMessageEventArgs(this);
e.InfoString = "Message was successfully sent!";
OnMsgSent(this,e);
}
}

but when I trie to compile this, I get the following error message:
"The event ,<my namespace>.<base class>.OnMsgSent'' can only appear on the
left hand side of += ...."

[I have doubt that this error appears where you register the event, can
you show us how you register the event too].
What is it I am doing wrong?

Thanks for hints.
Rainer



这篇关于事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆