MARQUE BURGESS MARQUE BURGESS - 1 year ago 100 Question

length can't be less than zero

I'm receiving the following error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Length cannot be less than zero.

This is the code producing the error:

'Parse coordinate string into coordinate values (degrees, minutes, seconds)
StartPtr = 0
StopPtr = DegreeString.IndexOf("°", StartPtr)
Degrees = Convert.ToDouble(DegreeString.Substring(StartPtr, StopPtr - StartPtr))

The error comes in the Degrees line and tells me my StopPtr is returning -1.

Answer Source

The Substring(startIndex, length) function is complaining that you passed a negative number as the length parameter. Which is invalid for obvious reasons (right?).

This means that StopPtr - StartPtr must be evaluating to something less than zero. You know StartPtr is exactly zero, because you just set it to zero. So StopPtr must be less than zero.

So that means DegreeString.IndexOf("°", StartPtr) is returning a negative number.

The IndexOf function returns -1 when the substring is not found.

So the problem must be that DegreeString does not contain the character "°".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download