i’ve some difficult to explain my problem, because i’m also not good in english :|!
I’ve the following code
namespace StighyGames.MyGames {
public class MyGames
{
...
...
CarClass myCar = new CarClass(...);
Main () {
MyGames game = MyGames(...);
}
}
Game is my “main” object (my application).
In my CarClass i’ve some code …
Now, inside myCar (object) i would like to call a function (a method) contained the main class MyGames.. the method “GameOver” .
This, because, if my car “explode” i’ve to call the GameOver Method.
But GameOver method can’t be “child” of myCar … because GameOver is a method of the game…
Ok.. hope to be explained, my question is: “I don’t know how to call a method of the main object class”..
Thank you for your help !
As everyone has answered you have several options. But not all the options are equally good.
Here my list.
1) Add an event to the Car object and let the Game register to it.
2) Pass a delegate to the Car object
3) Pass to the Car object a reference to a Game
4) Use static call.
All the answer except the first are “bad” 😉
Here the code. I’m using C# 4.0