CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2011
    Posts
    2

    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.

  2. #2
    Join Date
    Jun 2011
    Posts
    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.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: out of range exception (except it isn't)

    Quote Originally Posted by DataMiser View Post
    So does substring start at 0? I am acustomed to string functions starting at 1 ??
    In c# the string index starts at 0.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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
  •  





Click Here to Expand Forum to Full Width

Featured