1

How can I store a string command line argument in to a self defined string variable. As it tend to give "Array Index out of Range Bounds"...

Here is the code,

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

namespace Prac1_e
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a String and a number : ");
            Console.Read();
            string str = args[0];
            int n = Convert.ToInt32(args[1]);
            Console.WriteLine(str);
            Console.WriteLine(n);
            Console.ReadKey();
        }
    }
}
7
  • 1
    please show the input string that you have typed in Commented Aug 22, 2017 at 6:20
  • are you passing an argument to the application? Commented Aug 22, 2017 at 6:20
  • 1
    You are reading arg which has value when you execute your program with arguments in command line. When you want run-time arguments, you have to read from Console.Read(); itself. It has a return type of string. Commented Aug 22, 2017 at 6:26
  • @praty good catch. Modified my answer to include both options. Commented Aug 22, 2017 at 6:37
  • @praty " It has a return type of string" sorry, but no, it has a return type of int and it reads only the next character from the input stream. May be you meant ReadLine ?=! Commented Aug 22, 2017 at 6:39

4 Answers 4

1

You are attempting to access the first two command line argument (agrs[0] and args[1]), but aren't passing any. Just pass some arguments and you should be fine:

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

Comments

0

No error in this code. But you will get above error, when you execute the program incorrectly. this should be the correct way:

[program name] [argument 1] [argument 2] 

ex:

testprogram arg1 5

Comments

0

Arguments need to be passed via the command line in the format appname.exe "arg1" "arg2" etc. Also, since you are converting to an Int, you may want to validate that arg[1] is convertible to an Int.

    static void Main(string[] args)
    {
        Console.WriteLine("Enter a String and a number : ");
        Console.Read();
        if(args.Length >= 2)
        {
            string str = args[0];
            int n = Convert.ToInt32(args[1]);
            Console.WriteLine(str);
            Console.WriteLine(n);
            Console.ReadKey();
        }
        else
        {
            Console.WriteLine("No Args passed");
        }
    }

If you are looking to capture runtime parameters via command-line, you would need to do something like the following:

    static void Main(string[] args)
    {
        Console.WriteLine("Enter a String and a number, with a space between: ");
        string consoleRead = Console.ReadLine();
        string[] parsed = consoleRead.Split(' ');
        if (parsed.Length > 1)
        {
            string str = parsed[0];
            int n = Convert.ToInt32(parsed [1]);
            Console.WriteLine(str);
            Console.WriteLine(n);
            Console.ReadKey();
        }
    }

Comments

0

If you intend to read arguments passed in run-time from command line, you would need to read from Console object. You can try this code.

static void Main(string[] args)
    {
        Console.WriteLine("Enter a String and a number : ");

        string str = Console.ReadLine();
        int n = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(str);
        Console.WriteLine(n);
        Console.ReadKey();
    }

3 Comments

I would let @KamleshSharma confirm if this is what he wanted to achieve or if he really wanted to read a command line argument.
Edited my answer. Hope this is more descriptive than before.
definetely, it increases the quality of your answer. :)

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.