3

I have problem in the following C# program. there is no error when a code it but when I debug it put an exception that index is out of the range of array bound.

What is the problem here?

using System;
class secmain
{
    public static void squarearg(int i)
    {
        int m=i*i;
        Console.WriteLine("The Square of the argument is {0}",m);
    }
    static void Main(String[] param)
    {
     Console.WriteLine("this program will convert your string argument to int and display the square of the numbe");
     int k = Int32.Parse(param[0]);
      squarearg(k);
      }
}
Darren
  • 68,902
  • 24
  • 138
  • 144
Irfan Ul Haq
  • 1,065
  • 1
  • 11
  • 19
  • areyou sure you have pass an argument? – John Woo Feb 18 '13 at 11:42
  • try to pass a command line argument and the program should work. – wizzardz Feb 18 '13 at 11:42
  • 1
    I know this is only a simple application but may I also recommend that you give your variables meaningful names; 'i', 'm' and 'k' aren't very descriptive names, if this is educational, you may lose marks for that :) – RobJohnson Feb 18 '13 at 11:50

5 Answers5

9

Well debugging it (normally) won't get the command line parameter in the param that is why you are getting the exception.

You need to run its executable from the command line and pass the parameter from there, something like on your dos prompt, (cmd)

C:\yourProjectPath\bin\debug> yourExecutable.exe 2

(In above 2 is the command line argument that you will get in your param[0] as string)

Or you can use Console.ReadLine to get value from the console and then process that instead of command line parameters.

If you want to pass the argument during the debug process then check this question: How do I start a program with arguments when debugging?

Go to project properties and specify Command line argument under debug tab in Command Line Argument text box, like the following image.

enter image description here

The above will give you result 4 for argument 2

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
4

If you click project properties, under the Debug section, just below the Build, you can pass in arguments.

It will look like so:

/client:"Someclient.exe" 1 2 3

Which means main argument param won't be empty i.e. param.Count > 0

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
1

This is because of this statement int k = Int32.Parse(param[0]); you are not passing any value during the runtime. And you are trying to use the value from command-line argument
This will guide you how to pass command line argument in C#

Run your program this way

myfile.exe 125
asifsid88
  • 4,631
  • 20
  • 30
1

When using param it will expect that it was given in the command line. Try running it with something like "secmain.exe 4". Or you can go to Project Properties->Debug-> Start Options->Command line arguments and enter a value there.

misha
  • 2,760
  • 3
  • 28
  • 30
1
int k = Int32.Parse(param[0]);

I assume the error is there. You are not passing any parameters into the program.

You could do a check:

if (param.Length == 0)
{
   Console.WriteLine("No arguments have been passed into the program");
}

http://msdn.microsoft.com/en-us/library/aa664432(v=vs.71).aspx

Darren
  • 68,902
  • 24
  • 138
  • 144