Error en el formulario de beca por favor resuelva mis problemas.

programación


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)

Lo que he probado:

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)

Solución 1

Eche un vistazo a los siguientes ejemplos:

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);
	}
}

Nota 1)
Es cuestionable lanzar en ese caso una excepción. La mayoría de los demás idiomas simplemente devolverán los caracteres disponibles.

[Edit]

Nota *1), Gracias a Richard por su pista.
Algunos idiomas no arrojan una excepción en ese caso.

Espero que ayude.

コメント

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