4

I have a class named TestMaze. I have another class named DisplayHome which has a method called gameOver():

public void gameOver()
    {
        Console.Write("GAME OVER!");
        Console.Write("Play Again? Y/N");
        if(char.ToLower(Convert.ToChar(Console.Read())=='y')
            //Main()
        else
            Environment.Exit(1);
    }

How can I call the Main method?
PS. they have the same namespace. I just need to know how can I call the Main method again.

Reinan Contawi
  • 225
  • 1
  • 6
  • 13

3 Answers3

4

Refactor your code. Move whatever needs to be called into another function, and call it from both, main, and gameOver.

K Mehta
  • 10,323
  • 4
  • 46
  • 76
4

You should have a Play() method inside your Main... and GameOver() should call Play() if user enters 'y'.

c0deNinja
  • 3,956
  • 1
  • 29
  • 45
2

Assuming Main is a static class method (which I'd imagine it is) you can simply use MyClass.Main(/*relevant args*/) - beware of course that it's going to be a fresh instantiation, it won't share any non-static variable data.

A possibly better solution however would be to put all your code into a separate class which is invoked/instantiated from Main() - your program can then pass a boolean back to the actual executable Main which will be used to decide whether or not to exit or loop.

Olipro
  • 3,489
  • 19
  • 25
  • And make sure `Main` has a proper protection level. `protected static int Main` will cause some extra work – lastr2d2 Oct 23 '13 at 07:20