C#事件,如何提高它们?

Rob*_*Rob 2 c# events

我正在尝试学习在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!";
}
Run Code Online (Sandbox Code Playgroud)

和球类:

class ballClass
{
    public event EventHandler BallInPlay;

    public void onHit()
    {
        this.BallInPlay ///// ??? how should i raise this event?
    }
}
Run Code Online (Sandbox Code Playgroud)

在电话中我无法理解,我该如何举起活动?有任何想法吗?... :)

谢谢!

Chr*_*mes 9

public void onHit()
{
   if(BallInPlay != null)
     BallInPlay(this, new EventArgs());
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为`EventArgs.Empty`会比`new EventArgs()`更好 (3认同)