奖学金表格错误请解决我的问题


System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.Substring(Int32 startIndex, Int32 length) at ScholarFormDetail1112122.btn_submit_Click(Object sender, EventArgs e)

我尝试过的:

System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.Substring(Int32 startIndex, Int32 length) at ScholarFormDetail1112122.btn_submit_Click(Object sender, EventArgs e)

解决方案1

看看下面的例子:

public class Program
{
	public static void Main()
	{
		string test= "0123456789";
		
		// 1.) Will fail, because max. Index will be 9
		// string subString0= test.Substring(10, 1);
		
		// 2.) Ok
		string subString1= test.Substring(9, 1);
		
		// 3.) Will fail because  there is only one character available. See note *1)
		// string subString2= test.Substring(9, 100);

		// 4.) Ok
		string subString3= test.Substring(9, 1);
	}
}

注*1)
在这种情况下抛出异常是值得怀疑的。 大多数其他语言只会返回可用的字符。

[Edit]

注*1),感谢Richard的提示。
有些语言在这种情况下不会抛出异常。

我希望它有帮助。

コメント

タイトルとURLをコピーしました