IndexOutOfRangeException ?
In this following code i am receiving and out of range exception.
private void btnRoll_Click(object sender, EventArgs e)
{
int success4 = 0;
int success6 = 0;
int success8 = 0;
int success10 = 0;
int success20 = 0;
int botch4 = 0;
int botch6 = 0;
int botch8 = 0;
int botch10 = 0;
int botch20 = 0;
if (cbnd4.SelectedIndex != 0)
{
int value = 4;
int arraySize = (int)cbnd4.SelectedIndex;
int[] refArray = randomNumber(value, arraySize);
foreach (int i in refArray)
{
if (cbGame.SelectedIndex == 1)
{
if (refArray[i] >= 2)
{
success4++;
}
if (refArray[i] == 1)
{
botch4++;
}
}
if (cbGame.SelectedIndex == 2)
{
if(refArray[i] >= 2)
{
success4++;
}
if (refArray[i] == 1)
{
botch4++;
}
}
}
}
/* if (cbmd4.SelectedIndex != 0)
{
}
*/
if (cbnd6.SelectedIndex != 0)
{
int value = 6;
int arrySize = (int)cbnd6.SelectedIndex;
int[] refArray = randomNumber(value, arrySize);
foreach (int i in refArray)
{
if (cbGame.SelectedIndex == 1)
{
if (refArray[i] >= 4)
{
success6++;
} if (refArray[i] == 1)
{
botch6++;
}
}
if (cbGame.SelectedIndex == 2)
{
if (refArray[i] >= 4)
{
success6++;
}
if (refArray[i] == 1)
{
botch6++;
}
}
}
}
if (cbnd8.SelectedIndex != 0)
{
int value = 8;
int arrySize = (int)cbnd8.SelectedIndex;
int[] refArray = randomNumber(value, arrySize);
foreach (int i in refArray)
{
if (cbGame.SelectedIndex == 1)
{
if (refArray[i] >= 5)
{
success4++;
}
if (refArray[i] == 1)
{
botch8++;
}
}
if (cbGame.SelectedIndex == 2)
{
if (refArray[i] >= 5)
{
success4++;
}
if (refArray[i] == 1)
{
botch8++;
}
}
}
}
if (cbnd10.SelectedIndex != 0)
{
int value = 10;
int arrySize = (int)cbnd10.SelectedIndex;
int[] refArray = randomNumber(value, arrySize);
foreach (int i in re开发者_如何转开发fArray)
{
if (cbGame.SelectedIndex == 1)
{
if (refArray[i] >= 7)
{
success10++;
}
if (refArray[i] == 1)
{
botch10++;
}
}
if (cbGame.SelectedIndex == 2)
{
if (refArray[i] >= 7)
{
success10++;
}
if (refArray[i] == 1)
{
botch10++;
}
}
}
}
if (cbnd20.SelectedIndex != 0)
{
int value = 20;
int arrySize = (int)cbnd20.SelectedIndex;
int[] refArray = randomNumber(value, arrySize);
foreach (int i in refArray)
{
if (cbGame.SelectedIndex == 1)
{
if (refArray[i] >= 16)
{
success20++;
}
if (refArray[i] == 1)
{
botch20++;
}
}
if (cbGame.SelectedIndex == 2)
{
if (refArray[i] >= 7)
{
success20++;
}
if (refArray[i] == 1)
{
botch20++;
}
}
}
}
lBotch_Result.Text = Convert.ToString(botch4 + botch6 + botch8 + botch10 + botch20);
lSuccess_Result.Text = Convert.ToString(success4 + success6 + success8 + success10 + success20);
MessageBox.Show("d4 successes: " +
success4.ToString() +
"\r\nd6 Successes: " +
success6.ToString() +
"\r\nd8 Successes: " +
success8.ToString() +
"\r\nd10 Successes: " +
success10.ToString() +
"\r\nd20 Successes: " +
success20.ToString() +
"\r\nd4 Botches: " +
botch4.ToString() +
"\r\nd6 Botches: " +
botch6.ToString() +
"\r\nd8 Botches: " +
botch8.ToString() +
"\r\nd10 Botches: " +
botch10.ToString() +
"\r\nd20 Botches: " +
botch20.ToString());
}
The Out of ranged exception occurs when if(refArray[i] >= 7) and the refArray.Length contains an odd int value.
here is the Exception Output:
System.IndexOutOfRangeException was unhandled
Message="IndexOutOfRangeException" StackTrace: at Table_Top_Game_Dice.Form1.btnRoll_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam) at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) at System.Windows.Forms.Application.Run(Form fm) at Table_Top_Game_Dice.Program.Main()
any advice here would be greatly appreciated. i have been pounding my head against the wall for 5 hours trying to fix this.
oh, the refArray gets it values from the following function: (if it helps)
private int[] randomNumber(int value, int arraySize)
{
int[] randArray = new int[arraySize];
maxValue = value;
Random rand = new Random();
for (int i = 0; i < arraySize; i++)
{
randArray[i] = rand.Next(minValue, maxValue);
}
return randArray;
}
You are obviously trying to access an array element beyond the end of the array.
The randomNumber()
method generates an array of random numbers where the size of the array and the maximum value are independent. Therefore it might return { 1, 7, 13 }
if called with arraySize
3 and value
20.
Then you iterate over the array using foreach (int i in refArray)
. In consequence there will be three iterations with i
set to 1, then 7, and finally 13.
So if you access the array using refArray[i]
you try to access the array elements and indexes 1, 7, and 13 and therefore get an IndexOutOfRangeException
in the second iteration because you try to access the element at index 7 while the array contains only 3 elements.
Did you intend for (int i = 0; i < refArray.Length; i++)
instead of the foreach
loop?
Isn't this piece of code able to produce an int that is equal to the size of the array and subsequently cause an outofrange exception when reading that item from the array by i.
int[] refArray = randomNumber(value, arraySize);
foreach (int i in refArray)
{
if (cbGame.SelectedIndex == 1)
{
if (refArray[i] >= 2)
{
success4++;
}
if (refArray[i] == 1)
{
botch4++;
}
}
I didn't found the error, but you are repeating a lot of code. Try to encapsulate all these code inside of each "if (cbndXX.SelectedIndex != YY) inside a function.
Something like this:
private void RefactorizedFunction(ComboBox cmb, ComboBox cbGame, ref int success, ref int botch, int value, int maxsuxcess)
{
var arraySize = (int)cmb.SelectedIndex;
int[] refArray = randomNumber(value, arraySize);
foreach (int i in refArray) //WARNING HERE...
{
if (cbGame.SelectedIndex == 1)
{
if (refArray[i] >= maxsuxcess)
{
success++;
}
if (refArray[i] == 1)
{
botch++;
}
}
if (cbGame.SelectedIndex != 2) continue;
if (refArray[i] >= maxsuxcess)
{
success++;
}
if (refArray[i] == 1)
{
botch++;
}
}
}
It is not tested, and it will not solve your problem, but I'm sure your code will be easier to debug. There is other ways to improve your code, like using a dictionary or arrays instead of all these successXX and botchXX vars but... step by step.
精彩评论