I am trying to return the last value of a list:
public int GetControllerState()
{
// return the last controllerState in the list
if (controllerStateList != null)
{
if (controllerStateList.Count > 0)
{
return (int)controllerStateList[controllerStateList.Count - 1];
}
else
{
return 0;
}
}
else
{
return 0;
}
}
The above method sometimes throws
"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection."
and breaks on the return statement of the above code, the thing is, when I check the values in the debugger, i can see:
|controllerStateList.Count = 2|
|controllerStateList.Count - 1 = 1|
and |controllerStateList[1] = PERFORMING_RUN|
So, this just shouldnt be happening! Not sure if I am being stupid or not, thanks.