J
jd
Hi guys, I have a very small method which calculates the total of 1 of
3 possible arrays. The arrays are stored locally, and the index to
which array we wish to total is passed as a parameter:
private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int Total = 0;
switch (SomeArrayID)
{
case 1:
{
for (int i = 0; i < Array1.Length; i++)
{ Total += Array1; }
break;
}
case 2:
{
for (int i = 0; i < Array2.Length; i++)
{ Total += Array2; }
break;
}
case 3:
{
for (int i = 0; i < Array3.Length; i++)
{ Total += Array3; }
break;
}
}
return Total;
}
At the moment I'm checking the input paramter to see which array I
should be totalling, then looping through it, but I'd prefer to do
some kind of check on which array to use first, then set a pointer
accordingly and then only use one loop.
Has anyone got any suggestions on how to do this?
TIA
3 possible arrays. The arrays are stored locally, and the index to
which array we wish to total is passed as a parameter:
private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int Total = 0;
switch (SomeArrayID)
{
case 1:
{
for (int i = 0; i < Array1.Length; i++)
{ Total += Array1; }
break;
}
case 2:
{
for (int i = 0; i < Array2.Length; i++)
{ Total += Array2; }
break;
}
case 3:
{
for (int i = 0; i < Array3.Length; i++)
{ Total += Array3; }
break;
}
}
return Total;
}
At the moment I'm checking the input paramter to see which array I
should be totalling, then looping through it, but I'd prefer to do
some kind of check on which array to use first, then set a pointer
accordingly and then only use one loop.
Has anyone got any suggestions on how to do this?
TIA