You need to subscribe to the event using a handler. For instance:
BallClass ball = new BallClass(); ball.BallInPlay += BallInPlayHandler; // Now when ball.OnHit is called for whatever reason, BallInPlayHandler // will get called ... private void BallInPlayHandler(Object sender, EventArgs e) { // React to the event here }
For more information, you can read my article on events and delegates .
Please note that I fixed the BallClass and OnHit above - it is a good idea to use standard .NET naming conventions so that your code fits better into the code around it, and to make it more readable for others.
One note: the invalidation check that you have received so far is not thread safe. The last subscriber can unsubscribe after if , but before the call. Safe version:
public void OnHit() { EventHandler handler = BallInPlay; if (handler != null) { handler(this, new EventArgs()); } else { MessageBox.Show("null!"); } }
This is not guaranteed to be used by the latest subscribers (since there is no memory barrier), but it is guaranteed that it will not rule out a NullReference exception due to race conditions.
source share