-3

So I am making a C# Console Program that is a simple calculator, I am just learning C#.

Here is where I want to call main:

if (info.Key == ConsoleKey.Escape)
{
    Environment.Exit(0);
}
else
{
}

I want to call main for the Addition, Subtraction, Multiplication and Division classes so it goes back to the start to where it asks 'Press 'A' for Addition' etc.

I tried putting "Main();" in else but it gives me and error saying "There is no argument given that corresponds to the required formal parameter 'args' of 'Program.Main(String[])"

How could I go about calling main in this class so it goes to the start of main?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Drips
  • 1
  • 1
  • 2
  • Why do you want to call Main? Have main call another function to start your app. You can then call that function instead of Main. – Steve Oct 11 '15 at 11:52
  • 2
    Possible duplicate of [Calling Main( ) from another class](http://stackoverflow.com/questions/6723558/calling-main-from-another-class) – Reuben Mallaby Oct 11 '15 at 11:52

1 Answers1

6

You wouldn't call Main yourself it's used as an entry-point into the application. Usually you'd call out to the other methods, for example:

static void Main(string[] args)
{
   while (true) 
   {
        Console.Write("> ");
        string command = Console.ReadLine().ToLower();

        if (command == "add")
        {
            Add(); // Call our Add method
        }
        else if (command == "subtract")
        {
            Subtract(); // Call our Subtract method
        }
        else if (command == "multiply")
        {
            Multiple(); // Call our Multiply method
        }
        else if (command == "exit")
        {
            break; // Break the loop
        }
   }
}

static void Add()
{
    // to-be-implemented
}

static void Subtract()
{
    // to-be-implemented
}

static void Multiply()
{
    // to-be-implemented
}

Another thing to note here is it's Main(string[] args), the args parameter contains an array of arguments passed on the command line to the console application.

If you were to call Main yourself you would need to pass a value to this, for example:

Main(null); // No array
Main(new string[0]); // An empty array
Main(new string[] {}); // Another empty array
Main(new string[] { "Something" }); // An array with a single entry
Zephyr
  • 314
  • 2
  • 8
Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • Whilst this works, I think it would be much better to have a method along the lines of `GetCommand` rather than placing the logic in `Main` – Steve Oct 11 '15 at 11:57
  • @DarrenYoung It's only a quick example. – Lloyd Oct 11 '15 at 11:58