I have established an array and a large textbox which displays the array. Basically, you enter a name, 4 quarterly sales figures and it calculates the yearly total. I am now starting on the sort button. This would sort given columns numbers from most to least. I have gotten an if statement that looks like it should work for column five (the total column). I keep getting an out of range exception and am not sure why. Any ideas out there? I am sure this is a simple problem I am not seeing.
do {
swapFlag = false;
for (n=0; n < lastIndexUsed; n++)
{
if (quarters[n, sortCol] < quarters[n+1, sortCol])
{
//Column 5
temp = quarters[n,5];
quarters[n,5] = quarters[n +1, 3];
quarters[n +1, 3] = temp;
//swapflag
swapFlag = true;
}
}
} while (swapFlag);
This shows how I get 0-5 for sortCol:
if (rbRegion.Checked)
sortCol = 0;
if (rb1.Checked)
sortCol = 1;
if (rb2.Checked)
sortCol = 2;
if (rb3.Checked)
sortCol = 3;
if (rb4.Checked)
sortCol = 4;
if (rbTotal.Checked)
sortCol = 5;
Button Variables:
int n;
int temp;
int sortCol = 0;
string ntemp;
bool swapFlag;
Global Variables
int lastIndexUsed = -1;
int[,] quarters = new int[10, 5];
string[] Branch = new string[10];
quartersis defined assince arrays are zero based this gives you index
0...4on the second dimension but you are trying to access index5: