"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
Go Back  Xtreme Visual Basic Talk > > > "IndexOutOfRange" exception when working with a String Array.


Reply
 
Thread Tools Display Modes
  #1  
Old 06-17-2006, 09:42 PM
CrimsonWolf82 CrimsonWolf82 is offline
Newcomer
 
Join Date: May 2006
Posts: 3
Unhappy "IndexOutOfRange" exception when working with a String Array.


I need help with an "IndexOutOfRange" exception.

The following code is intended to output the list of files (with the .txt extension) in a folder specified by the user in the "txtFolder" text area into a label spaces (named lblOutput1) on the window.
NOTE: since the folder is likely to contain more then 1 .txt file, it is intended to continuesly overwrite the text in the label space untill it gets to the last file listed.
It is supposed to determine if it is at the end of the file list by checking the data in the subscript, and if the data = "" (blank) then it is finished.
Code:
        Dim strfiles(1000) As String
        Dim intFilnum As Integer = 1
        strfiles = IO.Directory.GetFiles(txtFolder.Text, "*.txt")

        Do
                    lblOutput1.Text = strfiles(intFilnum)
                    intFilnum = intFilnum + 1
        Loop Until strfiles(intFilnum) = ""
but it keeps generating an "IndexOutOfRange" exception.
after disecting this I've determined that even though I specified the array to have 1000 subscripts, it doesn't prepare any more then the number of txt files in the folder, therefore, when I try to check the data of subscript (# of txtfiles in folder + 1) it generates this error.
Is there a workarround or some way that I could specify for it to make one extra subscript then the number of txt files in the given folder?
Reply With Quote
  #2  
Old 06-19-2006, 08:24 AM
shaul_ahuva"IndexOutOfRange" exception when working with a String Array. shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Arrays in .NET are 0-based (not 1-based), and the number you specify when declaring an array is the upper-bound, not the length

Also, you don't need to create the strfiles array (i.e. specifying a length) - the Directory.GetFiles method will return the array you need:
Code:
'Creates a new array with 1001 elements. Dim strfiles(1000) As String 'Creates an array the length of the number of files matching the pattern. 'Once strfiles is assigned to the return value of Directory.GetFiles, the initial 'array you created above will be deleted since it's no longer in use. strfiles = IO.Directory.GetFiles(txtFolder.Text, "*.txt")
As far as constraining the loop, just loop until intFileNum = strfiles.Length - 1 in a For loop:
Code:
Dim strfiles() As String = IO.Directory.GetFiles(txtFolder.Text, "*.txt") For intFileNumber As Integer = 0 To strfiles.Length - 1 lblOutput1.Text = strfiles(intFilnum) Next
Using the Do loop as is could cause an IndexOutOfRange exception to be thrown if a 0-length array is returned from Directory.GetFiles.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array. "IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
 
"IndexOutOfRange" exception when working with a String Array.
"IndexOutOfRange" exception when working with a String Array.
 
-->