Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Index was outside the bounds of the array...?

Status
Not open for further replies.

nickmollberg

Programmer
Aug 2, 2004
29
0
0
US
This loop:
Code:
 for(int counter=0; counter <= tempArray.Length;counter++)
				{
				try
				 {
					 fdp.Parse( tempArray[counter], FormTypeSys, SectionSys, ScreenSys, QSys );
				 }
				catch( SqlException sqx )
				 {
					 throw new FormDesignParserException( "Error executing SQL: " + sqx.Message + " :: " + sqx.StackTrace );
				 }
				}

Gives me this error:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in whileStatement.exe

Additional information: Index was outside the bounds of the array.


It is looking througha textbox, parsing the contents out one line at a time... it seems to crash wehn it gets to the end of the textbox.

How can I stop it before this happens?
 
Modify the first line <= ----> <
Code:
for(int counter=0; counter < tempArray.Length;counter++)
-obislavu-
 
Or you could do this ...

Code:
int counter=0; counter <= tempArray.GetUpperBound(0);counter++

Both answers provided are correct, the difference is the zero based index. If tempArray has 4 values, they're 0, 1, 2, and 3. tempArray.Length returns 4 - one greater than the index. tempArray.GetUpperBound(0) returns the index value, not the count.

______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top