2

I want to pass two parameters to the following code, by giving numbers not from command line in debug

For example I want pass 4 4 to the code using entered values from user not fixed in command line to the :

class Program
{
    static void Main(string[] args  )
    {
        if (args.Length > 0)
            new Program(int.Parse(args[0] ));

        Console.ReadLine();
        Console.ReadLine();
        Console.Read();
        Console.ReadKey();
    }
}
5
  • 1
    Probably use the project properties to set the command line arguments. msdn.microsoft.com/en-us/library/vstudio/… But I'm not sure if that is what you're asking. Commented Apr 14, 2014 at 20:47
  • Are you asking how to accept user input (Console.ReadLine)? Or how to pass command line args when debugging? Commented Apr 14, 2014 at 20:47
  • i want use the values from console.readline not from debugging Commented Apr 14, 2014 at 20:49
  • In case you would need to support a bit more complex command line argument structures, I would recommend you docopt.org which provides command line parsing for multiple languages incl C, C++ etc. But do not get distracted by this too early, for your scenario there are for sure much simpler methods. Commented Apr 14, 2014 at 20:56
  • Ummm.... why are you creating a new Program class? That seems like a bad idea... Plus you aren't doing anything with it. Commented Apr 14, 2014 at 21:06

1 Answer 1

2

To get user input in "arg" format, you use Console.ReadLine():

string[] userArgs = Console.ReadLine().Split(' ');

The Console.ReadLine gets the entire input string, then to get the array we call .Split(). I split on spaces just like standard command line. Other delimiters are also possible, just pass in a different character to the Split function.

Of course, you could stick to using normal command line args and have the user enter them as part of the program call (not normally done on Windows since few people use command prompt, but it would work).

MSDN for Split in case you are interested!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.