Click here to Skip to main content
14,695,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning to write C# programs and started off with msdn C# tutorials. I am already stuck :) with a problem with my first program (Hello World).

// Hello3.cs
// arguments: A B C D
using System;

public class Hello3
{
   public static void Main(string[] args)
   {
      Console.WriteLine("Hello, World!");
      Console.WriteLine("You entered the following {0} command line arguments:",
         args.Length );
      for (int i=0; i < args.Length; i++)
      {
         Console.WriteLine("{0}", args[i]);
      }
   }
}


When I execute the above code, I expect a console window to open up for me to type in arguments, but it executes and the window dissapears even before I can see what is there in it. I have no idea what to do??????

I inserted the "Console.ReadKey()", but still the window doesnt stay on execution, it dissapears???
Posted
Updated 18-Apr-10 8:42am
v2

The proces executes, displays the results, and cleans up. The easy way out is to wait for a keypress;
for (int i=0; i < args.Length; i++)
{
   Console.WriteLine("{0}", args[i]);
   Console.ReadKey();
}


--edit;
As an alternative, you can run your code outside the debugger; if you hit Ctrl-F5, the app should be launched and it would ask to press any key before it exits.


Good luck :)
   
v3
Well, arguments are not entered in the Command Prompt that opens up rather they are passed while calling your exe from command line or Run dialog.

If you want to pass command line arguments from Visual Studio itself (usually done for testing command line utilities) then follow the following steps:
1. Open Properties for your solution.
2. Select Debug tab.
3. In Start Options, provide the arguments that you want to pass.

It would work perfectly after that.
   
Just to add to what the others have said, it is worth adding a prompt so that you know when it has finished:
public static void Main(string[] args)
{
   Console.WriteLine("Hello, World!");
   Console.WriteLine("You entered the following {0} command line arguments:",
      args.Length );
   for (int i=0; i < args.Length; i++)
   {
      Console.WriteLine("{0}", args[i]);
   }
   Console.Write("[End: Press any key to exit]");
   Console.ReadKey();
}
It is always a good idea to let your user (even if it is only you) know you are waiting for him to do something! Other than that, not a bad effort for a first program at all. :-D
   
jbjoat wrote:
I inserted the "Console.ReadKey()", but still the window doesnt stay on execution, it dissapears???

I'm guessing you've placed the line in the for loop. Place it outside the loop, like so:

for (int i = 0; i < args.Length; i++)
{
    Console.WriteLine("{0}", args[i]);
}
Console.ReadKey();
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900