【解決方法】奨学金フォームのエラーの問題を解決してください


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をコピーしました