System.IndexOutOfRangeException: Index was out of bounds

情到浓时终转凉″ 提交于 2019-12-02 14:23:14

Problem : When you run it from Visual Studio you are providing the arguments but when you run the program directly by double clicking on it you can not provide the arguments as it willbe invoked directly.

Solution : you need to provide command line arguments properly, follow the below steps to run your program from command line

Step 1: goto Command Prompt

Step 2: goto your program exe file path

Step 3: now execute the program by providing commandline arguments as below:

c:\myprogrampath\program.exe 12

Try This code to avoid Exceptions:

if(args.Length>0)
{
    int tala = Convert.ToInt32(args[0]);
    MultiplicationTable test = new MultiplicationTable(tala);
    Console.ReadLine();
}
else
{
    Console.WriteLine("No Command Line Arguments - Quiting");
}

Yes,

as the poster before said, either you have to pass arguments to your program or you have to check, if args is not null with an if-statement and "catch" this error.

if(args) {
//here your code
}

Alternatively, you could try an try - catch statement:

try {
   //here you read the arguments and pass to a variable
}
catch(System.IndexOutOfRangeException) {
     //other codepart
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!