例外:索引超出了数组的范围。 [英] Exception : Index was outside the bounds of the array.

查看:88
本文介绍了例外:索引超出了数组的范围。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个在控制台和ui.both运行的应用程序。使用cmd表示仅运行。在defaut中,即采用控制台模式。当给出如 C的推荐时:\ Raju \ selectlected \bin \Release> selectedpath.exe tis是推荐提示。并给出路径,如 C:\ Rajh \ selectlected \ bin \ Release> selectedpath.exe ui 。这是开放的ui和run.this工作良好。但我写这个 C:\ Raju \ selectedpath \ bin \ Release> selectedpath.exe 然后给出错误索引是在数组范围之外。我在下面写代码也请解决这个问题

代码:

Hi,
I am create one application that is working in console and ui.both r run using cmd commends only.in defaut ie is take console mode.when given commend like C:\Raju\selectedpath\bin\Release>selectedpath.exe tis is on commend prompt. and give path like C:\Raj\selectedpath\bin\Release>selectedpath.exe ui. this is open ui and run.this working good.but i am writ this one C:\Raju\selectedpath\bin\Release>selectedpath.exe then given error Index was outside the bounds of the array. I am write code below also please solve this problem
Code :

[STAThread]
       static void Main(string [] args)
       {

               if (args[0] == "ui")
               {
                   Application.Run(new selectedpath.Form1());
               }
               else
               {
                   Console.WriteLine("dfdd");
               }



}

}

}


}
}
}

推荐答案

好吧, args [] 是一个包含所有参数的数组。在没有参数的情况下调用应用程序时它包含多少个参数?

首先检查数组的 Length ,然后再尝试访问其第一个成员。
Well, args[] is an array conating all the arguments. How many arguments does it contain when you do call your application without arguments?
So check for the Length of the array first, before you try to access its first member.


是的,Bernhard在解决方案1中说,在访问其第一个索引值之前检查您的数组长度。

尝试以下



Yes as per Bernhard said in solution 1, Check your array length before accessing its first index value.
Try the following

if(args.length() > 0)
{	    
  if (args[0] == "ui")
    {
      Application.Run(new selectedpath.Form1());
    }
   else
    {
      Console.WriteLine("dfdd");
    }
}





问候,

BlueSathish



Regards,
BlueSathish


这篇关于例外:索引超出了数组的范围。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆