我在運行時遇到了這個錯誤。 如果你能解釋這對我的編碼有很大的幫助。謝謝。我的代碼錯誤「System.IndexOutOfRangeException」
public int timePeriodInSeconds(String timeInFormat)
{
System.Diagnostics.Debug.WriteLine("timePeriodInSeconds timeInFormat= " + timeInFormat);
String[] timeFactors = timeInFormat.Split(':');
System.Diagnostics.Debug.WriteLine("timePeriodInSeconds timeFactors[0]" + timeFactors[0]);
System.Diagnostics.Debug.WriteLine("timePeriodInSeconds timeFactors[1]" + timeFactors[1]);
System.Diagnostics.Debug.WriteLine("timePeriodInSeconds timeFactors[2]" + timeFactors[2]);
int hours = Convert.ToInt32(timeFactors[0]);
int minutes = Convert.ToInt32(timeFactors[1]);
int seconds = Convert.ToInt32(timeFactors[2]);
System.Diagnostics.Debug.WriteLine("timePeriodInSeconds hours" + hours);
System.Diagnostics.Debug.WriteLine("timePeriodInSeconds minutes" + minutes);
System.Diagnostics.Debug.WriteLine("timePeriodInSeconds seconds" + seconds);
return (hours * 60 * 60 + minutes * 60 + seconds);
}
'timeFactors'只有1個位置,並且您試圖訪問第二個(超出邊界)。只是調試你的代碼 – Petaflop
謝謝先生。我對這種語言很陌生。這個編碼部分是由我的朋友完成的。現在我想開發這個。所以這個錯誤發生了,你可以進一步解釋。 – SNP
如果您不熟悉該語言,我建議您購買一些書並開始使用。在這個特殊情況下,這應該有助於你理解發生了什麼:http://csharp.net-tutorials.com/basics/arrays/ – Petaflop