-1

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)
'Degrees
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.

Community
  • 1
  • 1
MBurg_22
  • 3
  • 3
  • Please read through [ask], and then see here to learn how to create a [mcve]. You've just dumped a ton of code with a vague *I can't really figure this out* sentence. – Ken White Aug 21 '16 at 23:59
  • I went back and adjusted the question some and took some of the coding out. I'm hoping I left enough for people to understand what the program is doing. – MBurg_22 Aug 22 '16 at 20:03

1 Answers1

1

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 "°".

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • I understand what your saying with indexOf returning -1. I was attempting to change: StartPtr = 0 StopPtr = Hubs(Index1Integer).Lat.IndexOf("°", StartPtr) degrees = Convert.ToDouble(Hubs(Index1Integer).Lat.Substring(StartPtr, StopPtr - StartPtr)) into the function above with the DegreeString being my input parameter. I'm confused with how it would be out of OutOfRange now. – MBurg_22 Aug 24 '16 at 02:11
  • @MARQUEBURGESS - It's because `DegreeString` does not contain `'°'`. That's the only reason. – Enigmativity Aug 24 '16 at 02:15
  • In order to understand what's happening, you need to set some breakpoints in your code and step through it, observing how the values of your variables change as the code executes. – Blorgbeard Aug 24 '16 at 02:30
  • Sorry. I went through it and understand what you guys are saying. I was looking right past it. – MBurg_22 Aug 24 '16 at 02:40
  • Appreciate your help – MBurg_22 Aug 24 '16 at 02:51
  • No problem, glad you sorted it. – Blorgbeard Aug 24 '16 at 02:52