Скажем, у меня есть class под названием Frog, он выглядит так:
public class Frog { public int Location { get; set; } public int JumpCount { get; set; } public void OnJump() { JumpCount++; } }
Мне нужна помощь с двумя вещами:
public event EventHandler Jump; public void OnJump() { EventHandler handler = Jump; if (null != handler) handler(this, EventArgs.Empty); }
затем
Frog frog = new Frog(); frog.Jump += new EventHandler(yourMethod); private void yourMethod(object s, EventArgs e) { Console.WriteLine("Frog has Jumped!"); }
@CQ: Почему вы создаете локальную копию pf Jump
? Кроме того, вы можете сохранить последующий тест, слегка изменив объявление события:
public event EventHandler Jump = delegate { }; public void OnJump() { Jump(this, EventArgs.Empty); }