我正在尝试学习在C#中引发和处理事件。
这是我的简单示例:
/////////////// creating a new BALL object ///
ballClass ball = new ballClass();
void button1_Click(object sender, EventArgs e)
{
// this should make the BALL object raise an event
ball.onHit();
label1.Text = "EVENT SEND";
}
// when event is fired, label text should change
void BallInPlayEvent(object sender, EventArgs e)
{
label2.Text = "EVENT FOUND!";
}
和球类:
class ballClass
{
public event EventHandler BallInPlay;
public void onHit()
{
this.BallInPlay ///// ??? how should i raise this event?
}
}
在电话中,我无法真正理解,该如何发起活动?有任何想法吗?... :)
谢谢!
最佳答案
public void onHit()
{
if(BallInPlay != null)
BallInPlay(this, new EventArgs());
}
关于c# - C#事件,如何引发它们?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4818824/