3

i am new in C# and working on Console Applications now a days i wrote the following code :

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch06Ex01
{
    class Program
    {
        static void Write()
        {
            Console.WriteLine("Please enter any string..!!");
        }

         static void Main(string[] args)
        {       

            Write();
            string name = Console.ReadLine();
            Write();
            string name1 = Console.ReadLine();
            Write();
            string name2 = Console.ReadLine();
            Write();
            string name3 = Console.ReadLine();

             Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

             Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
             string selectedOption = Console.ReadLine();

             if (selectedOption == "y")
             {
                 // howto call  static void Main(string[] args) here agin so that the program start itself from the start point

             }    

             //else if (selectedOption == "n")
            {
               //Terminate the Program
             }
             Console.ReadKey();
         }      

    }

}

Now at the point :

 if (selectedOption == "y")
             {
                 // howto call  static void Main(string[] args) here agin so that the program start itself from the start point

             }    

i wanted to reboot the program if User enter "y" and terminate it if the user enter "n", for this purpose firs of all i tried to use goto statement like :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch06Ex01
{
    class Program
    {
        static void Write()
        {
            Console.WriteLine("Please enter any string..!!");
        }

         static void Main(string[] args)
        {
            StartPoint;

            Write();
            string name = Console.ReadLine();
            Write();
            string name1 = Console.ReadLine();
            Write();
            string name2 = Console.ReadLine();
            Write();
            string name3 = Console.ReadLine();

             Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

             Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
             string selectedOption = Console.ReadLine();

             if (selectedOption == "y")
             {
                 // howto call  static void Main(string[] args) here agin so that the program start itself from the start point
                 goto StartPoint;
             }    

             //else if (selectedOption == "n")
             Console.ReadKey();
         }      

    }
}

but it didn't work for me at StartPoint; it gives error that

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement   C:\Users\Ahsan\Desktop\C# for Beginners Examples\Ch06Ex01\Ch06Ex01\Program.cs   18  13  Ch06Ex01

then i tried to call main function itself at point

 if (selectedOption == "y")
         {
             // howto call  static void Main(string[] args) here agin so that the program start itself from the start point

         }    

but it gives me a lot of error here , don' know how to do it now, can any one help me out ? don't know how to do this thing work.... calling " static void Main(string[] args) "in the class again would be preferred....

Ahsan Hussain
  • 952
  • 4
  • 21
  • 42
  • 1
    You can use `while(Console.ReadKey()!='') {code here}` – kravasb Oct 29 '13 at 10:44
  • 2
    Static void Main(string[] args) is the entry point of a C# console application or windows application. When the application is started, the Main method is the first method that is invoked. Why do you want recall it again? – Doro Oct 29 '13 at 10:47
  • 2
    While people are giving you an answer to what you've asked (how to call your own program root) I'd be careful about doing so. Your application will not end until the thread traverses the full code block of `Main`. In this case the first call to `Main` will not resolve until the inner `main` completes. You could run the risk of a stack overflow if you continually recurse like this. However, `Main` having a `void` return might cause some tail-call optimization to occur. A `while` loop is the idiomatic C# approach to this problem. – Adam Kewley Oct 29 '13 at 10:48

5 Answers5

10

Firstly, your label is incorrect. The end of a label should have a colon character :.. so your label should have been this:

StartPoint:

However:

You should just loop until a condition is met. In this case.. the condition is to not restart:

bool running = true;

while (running) {
    /* 
     * all of your other code
     * should go here
     */
    if (selectedOption != "y") {
        running = false;
    }
}
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
5

You realy should not use a goto or call your Main again (recursion), do while is a better solution to repeat your logic muliple times:

    string selectedOption;
    do {
        Write();
        string name = Console.ReadLine();
        Write();
        string name1 = Console.ReadLine();
        Write();
        string name2 = Console.ReadLine();
        Write();
        string name3 = Console.ReadLine();

         Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

         Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
         selectedOption = Console.ReadLine();
      } while (selectedOption == "y")
      Console.ReadKey();
Peter
  • 27,590
  • 8
  • 64
  • 84
1

Just call the method again, passing in the same arguments that where passed in initially, also try to avoid using goto if you can:

if (selectedOption == "y")
{
    // howto call  static void Main(string[] args) here agin so that the program start itself from the start point
    Main(args);
}    
Community
  • 1
  • 1
JMK
  • 27,273
  • 52
  • 163
  • 280
  • 3
    @OP: You should be aware that this could potentially cause a stackoverflow.. given enough recursions. This is not really the correct way to handle the situation you have. I can't imagine a recursive entry point is a good thing. – Simon Whitehead Oct 29 '13 at 11:01
  • 2
    @OP I agree, Simons answer is a better solution, this is just a quick answer to your question. – JMK Oct 29 '13 at 11:02
1

Try put the code that you will execute outside the main class in another method like

void execute()
{
        Write();
        string name = Console.ReadLine();
        Write();
        string name1 = Console.ReadLine();
        Write();
        string name2 = Console.ReadLine();
        Write();
        string name3 = Console.ReadLine();

         Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

         Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
         string selectedOption = Console.ReadLine();

}

then in the main

static void Main(string[] args)
{
    bool run = true
    while(run){
       execute()
       Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
        selectedOption = Console.ReadLine();
        if (selectedOption == "n")
        {
             run = false;
        }    
    }
}
Alexandre
  • 498
  • 2
  • 12
0

something like this :

static void Main(string[] args)
            {      
                string selectedOption = "";

                 do 
                 {

                               ...........

                 }
                 while (selectedOption == "y")

                 if (selectedOption == "n")
                 {
                   //Terminate the Program
                  }

             } 
Zhenia
  • 3,939
  • 3
  • 15
  • 15