i have the following:
a game class
class Game
{
public event EventHandler GameOver;
public void go()
{
PlayerAliveEventArgs playerAlive = new PlayerAliveEventArgs(Alive);
GameOver(this, playerAlive);
}
}
then i have a class
public class PlayerAliveEventArgs : EventArgs
{
public bool Alive { get; set; }
public PlayerAliveEventArgs(bool deadOrAlive)
{
Alive = deadOrAlive;
}
}
in another class i tie a method to the event…
public void Form_Load()
{
game.GameOver += Form1_GameOverMethod; // it shows the error here.
it says no overload of this method matches System.Eventhandler
}
public void Form1_GameOverMethod(object sender, PlayerAliveEventArgs e)
{
if (!e.Alive)
{
GameTimer.Enabled = false;
gameOver = true;
Refresh();
}
}
The error is:
Method doesn’t exist in this context.
Why is that?
okay i made the following changes:
public void Form1_GameOverMethod(object sender, EventArgs e)
{
PlayerAliveEventArgs d = (PlayerAliveEventArgs)e;
if (!d.Alive)
{
}
}
is it okay now? or will it fire some problems when i run it (i want to save myself debugging latter on..)
GameOverMethoddoesn’t exist in that context indeed. what exists however (and that’s what you intended I suppose) isForm1_GameOverMethod.A couple more remarks. First, before firing an event, you should check whether someone has subscribed to it or not.
Second, I believe you should change you event declaration to be:
Hope this helps