473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

why argument out of range exception ?



string thingword=" testing hmmm";

string firstLetter,restOfWord;

foreach (string word in thingword.Split())

{

Console.WriteLine(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.WriteLine(firstLetter);

Console.WriteLine(restOfWord);

}

error:::

Unhandled Exception: System.ArgumentOutOfRangeException: Index and length
must r
efer to a location within the string.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at Class1.jimmy.Main(String[] what) in c:\documents and settings\jim\my
docum
ents\visual studio projects\consoleapplication1\class1.cs:line 43
Press any key to continue

Nov 15 '05 #1
2 17496
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" <fl***@masfd.com> wrote in message
news:3f**********@news.tm.net.my...


string thingword=" testing hmmm";

string firstLetter,restOfWord;

foreach (string word in thingword.Split())

{

Console.WriteLine(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.WriteLine(firstLetter);

Console.WriteLine(restOfWord);

}

error:::

Unhandled Exception: System.ArgumentOutOfRangeException: Index and length
must r
efer to a location within the string.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at Class1.jimmy.Main(String[] what) in c:\documents and settings\jim\my
docum
ents\visual studio projects\consoleapplication1\class1.cs:line 43
Press any key to continue

Nov 15 '05 #2
On your first iteration the value of word is ""

This will generate the error when you call Substring
Brian W

"smith flyers" <fl***@masfd.com> wrote in message
news:3f**********@news.tm.net.my...


string thingword=" testing hmmm";

string firstLetter,restOfWord;

foreach (string word in thingword.Split())

{

Console.WriteLine(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.WriteLine(firstLetter);

Console.WriteLine(restOfWord);

}

error:::

Unhandled Exception: System.ArgumentOutOfRangeException: Index and length
must r
efer to a location within the string.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at Class1.jimmy.Main(String[] what) in c:\documents and settings\jim\my
docum
ents\visual studio projects\consoleapplication1\class1.cs:line 43
Press any key to continue

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jim Cook | last post by:
We have a macro which takes various index constants as an argument and offsets into an array. The macro can be an Lvalue or Rvalue. The index is not zero based. I would like a compile time error...
3
by: Pengyu | last post by:
Whenever I call listView.RemoveAt(listView.Count-1), which remove the last item, I get an out of range exception. How to solve this problem? Thanks a lot, Pengyu.
0
by: Bill Johnson | last post by:
I have a further question regarding an archive post at: ...
4
by: Brad | last post by:
Now I am encountering my second very strange problem. I will describe the first one later. But my current problem lies with trying to bind to a combobox: Try If clubID <> "" And txtYear.Text <>...
0
by: Nick | last post by:
Hi people, I have a custom control combo box in a windows application that has a list of items to be displayed. there are currently 17 items that are bound to the table that acts as the data...
5
by: pcnerd | last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap & then displays the bitmap. When I run the program, I see the pixels being plotted down the left side of the form. When...
1
by: dedipya | last post by:
Environment W2003K with Oracle 10 client connected to oracle using oracle odp provider.orcle is on solaries machine. while filling the dataset .net is throughing the error "argument was out of the...
7
by: Pucca | last post by:
Hi: Below is the error I got from the 2 lines of code below. I don't understand why and how to correct it. The actionMenu.DropDownItems has 0 item in its collection at the time of the code. ...
2
by: GR M | last post by:
Hi, I was trying to add two quantities at different rows of same description i.e same product_name and same item_pack.1st I want to check the table if it contains the same description or not by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.