|
-
June 8th, 2011, 05:54 PM
#1
out of range exception (except it isn't)
i'm using visual studio 2010 and i'm getting the error:
Unhandled Exception: System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Occuring on a call to Substring(int,int);
here's my code (it's testing so there are magic numbers):
Code:
//No out of bounds exceptions
int temp = end - 150;
if (temp > 0)
{
source = source.Substring(temp, end);
}
source is approximately 32000 characters in length and end is looking at index ~18000 (and obviously temp is index less 150). I have tested it with the parameters such as:
Code:
source = source.Substring(0,end);
and there is no problem, but i have no idea why it would not work with temp. i have tried using a dummy string variable, but as i figured it has nothing to do with the assignment source = source.Substring...
No idea how this can be the case >_>
EDIT: nevermind, i misremembered Substring (second parameter is the number of characters you want to copy).
Last edited by jokul; June 8th, 2011 at 07:04 PM.
-
June 8th, 2011, 06:09 PM
#2
Re: out of range exception (except it isn't)
since i apparently can't edit my post, i do want to say that there was STILL an error when using 0 as a parameter, not that there wasn't.
-
June 8th, 2011, 06:43 PM
#3
Re: out of range exception (except it isn't)
So does substring start at 0? I am acustomed to string functions starting at 1 ??
Always use [code][/code] tags when posting code.
-
June 8th, 2011, 08:10 PM
#4
Re: out of range exception (except it isn't)
 Originally Posted by DataMiser
So does substring start at 0? I am acustomed to string functions starting at 1 ??
In c# the string index starts at 0.
-
June 8th, 2011, 08:17 PM
#5
Re: out of range exception (except it isn't)
Read up on the substring paarameters in msdn.
The 1st param is the starting index. The 2nd param is the lenght of the string to extract.
-
June 9th, 2011, 02:23 AM
#6
Re: out of range exception (except it isn't)
What if the end excedes lenght of the string?
Code:
source = source.Substring(0, Math.Min(end, source.Length));
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|