why argument out of range exception ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smith flyers

    why argument out of range exception ?



    string thingword=" testing hmmm";

    string firstLetter,res tOfWord;

    foreach (string word in thingword.Split ())

    {

    Console.WriteLi ne(word); // if this only use for output and the rest in
    the block is commented, no error !!!

    firstLetter = word.Substring( 0,1);

    restOfWord = word.Substring( 1, word.Length -1);

    Console.WriteLi ne(firstLetter) ;

    Console.WriteLi ne(restOfWord);

    }





    error:::



    Unhandled Exception: System.Argument OutOfRangeExcep tion: Index and length
    must r
    efer to a location within the string.
    Parameter name: length
    at System.String.S ubstring(Int32 startIndex, Int32 length)
    at Class1.jimmy.Ma in(String[] what) in c:\documents and settings\jim\my
    docum
    ents\visual studio projects\consol eapplication1\c lass1.cs:line 43
    Press any key to continue



  • Gareth

    #2
    Re: why argument out of range exception ?

    what if the word = a single character, i.e. word = "A"

    That would cause the word.Substring( 1, word.Length - 1); to fail with that
    error.


    "smith flyers" <flyer@masfd.co m> wrote in message
    news:3fafbec8$1 _1@news.tm.net. my...[color=blue]
    >
    >
    > string thingword=" testing hmmm";
    >
    > string firstLetter,res tOfWord;
    >
    > foreach (string word in thingword.Split ())
    >
    > {
    >
    > Console.WriteLi ne(word); // if this only use for output and the rest in
    > the block is commented, no error !!!
    >
    > firstLetter = word.Substring( 0,1);
    >
    > restOfWord = word.Substring( 1, word.Length -1);
    >
    > Console.WriteLi ne(firstLetter) ;
    >
    > Console.WriteLi ne(restOfWord);
    >
    > }
    >
    >
    >
    >
    >
    > error:::
    >
    >
    >
    > Unhandled Exception: System.Argument OutOfRangeExcep tion: Index and length
    > must r
    > efer to a location within the string.
    > Parameter name: length
    > at System.String.S ubstring(Int32 startIndex, Int32 length)
    > at Class1.jimmy.Ma in(String[] what) in c:\documents and settings\jim\my
    > docum
    > ents\visual studio projects\consol eapplication1\c lass1.cs:line 43
    > Press any key to continue
    >
    >
    >[/color]


    Comment

    • Brian W

      #3
      Re: why argument out of range exception ?

      On your first iteration the value of word is ""

      This will generate the error when you call Substring


      Brian W

      "smith flyers" <flyer@masfd.co m> wrote in message
      news:3fafbec8$1 _1@news.tm.net. my...[color=blue]
      >
      >
      > string thingword=" testing hmmm";
      >
      > string firstLetter,res tOfWord;
      >
      > foreach (string word in thingword.Split ())
      >
      > {
      >
      > Console.WriteLi ne(word); // if this only use for output and the rest in
      > the block is commented, no error !!!
      >
      > firstLetter = word.Substring( 0,1);
      >
      > restOfWord = word.Substring( 1, word.Length -1);
      >
      > Console.WriteLi ne(firstLetter) ;
      >
      > Console.WriteLi ne(restOfWord);
      >
      > }
      >
      >
      >
      >
      >
      > error:::
      >
      >
      >
      > Unhandled Exception: System.Argument OutOfRangeExcep tion: Index and length
      > must r
      > efer to a location within the string.
      > Parameter name: length
      > at System.String.S ubstring(Int32 startIndex, Int32 length)
      > at Class1.jimmy.Ma in(String[] what) in c:\documents and settings\jim\my
      > docum
      > ents\visual studio projects\consol eapplication1\c lass1.cs:line 43
      > Press any key to continue
      >
      >
      >[/color]


      Comment

      Working...