|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
overriding array sizeI have a program that reads a delimited text file and splits the input
string into string array elements: Dim Data(41) as String 'read semicolon delimited fields into string array Data = SrRead.ReadLine().Split(";".ToCharArray) I expect up to 40 elements to be read. But if the input data string has less than that and I try to access Data(40) I get a System.IndexOutOfRange exception. I thought that declaring the array size first would take care of that. I know I can GetLength on the array and know how big it is. But is there a way to statically set the array bounds so I don't need to know? Thanks Don't defind the number ahead of time. Let it find it for you when it reads
the first line. Dim fieldValues As String() 'Open file and read first line to determine how many fields 'there are. myReader = IO.File.OpenText(fileFullPath) fieldValues = myReader.ReadLine().Split(seperator) Show quoteHide quote "Dave Cullen" <nospam@mail.com> wrote in message news:458C3963.477C6482@mail.com... >I have a program that reads a delimited text file and splits the input > string into string array elements: > > Dim Data(41) as String > > 'read semicolon delimited fields into string array > Data = SrRead.ReadLine().Split(";".ToCharArray) > > I expect up to 40 elements to be read. But if the input data string has > less than that and I try to access Data(40) I get a > System.IndexOutOfRange exception. I thought that declaring the array > size first would take care of that. > > I know I can GetLength on the array and know how big it is. But is there > a way to statically set the array bounds so I don't need to know? > > Thanks "Dave Cullen" <nospam@mail.com> schrieb: .... is semantically equivalent to 'Dim Data() As String = New String(41) >I have a program that reads a delimited text file and splits the input > string into string array elements: > > Dim Data(41) as String {}'. > 'read semicolon delimited fields into string array 'Split' returns a dimensioned and filled array object. Thus you do not need > Data = SrRead.ReadLine().Split(";".ToCharArray) to dimension the array at its declaration: \\\ Dim Data() As String = SrRead.ReadLine.Split(...) /// -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Dim MyArr As Array
Dim MyString Ss String Dim i As Integer ' Build string MyString = "What;Are;You;Doing" ' Put it into an array - spliting each element with delimiter MyArr = Split(MyString, ";") ' Loop through array For i = LBound(MyArr) to UBound(MyArr) Dim MyRow as String = MyArr(i) Next The Grand Master Dave Cullen wrote: Show quoteHide quote > I have a program that reads a delimited text file and splits the input > string into string array elements: > > Dim Data(41) as String > > 'read semicolon delimited fields into string array > Data = SrRead.ReadLine().Split(";".ToCharArray) > > I expect up to 40 elements to be read. But if the input data string has > less than that and I try to access Data(40) I get a > System.IndexOutOfRange exception. I thought that declaring the array > size first would take care of that. > > I know I can GetLength on the array and know how big it is. But is there > a way to statically set the array bounds so I don't need to know? > > Thanks Dave Cullen wrote:
Show quoteHide quote > I have a program that reads a delimited text file and splits the input It's Kludgey in the extreme, but you could force the array size after > string into string array elements: > > Dim Data(41) as String > > 'read semicolon delimited fields into string array > Data = SrRead.ReadLine().Split(";".ToCharArray) > > I expect up to 40 elements to be read. But if the input data string has > less than that and I try to access Data(40) I get a > System.IndexOutOfRange exception. I thought that declaring the array > size first would take care of that. > > I know I can GetLength on the array and know how big it is. But is there > a way to statically set the array bounds so I don't need to know? you've loaded it, as in : Dim Data as String() _ = SrRead.ReadLine().Split(";"c) ReDim Preserve Data(40) I've yet to find a ".Net" equivalent of ReDim Preserve ... HTH, Phill W. Thank you Phill, that works. The other solutions posted would require me
to test the size of the array before attempting to read each element (tedious), or abort if the length was not what I expect. With the ReDim Preserve I get to have a valid array of the size expected (no crash & burn when I read element 40) and also have backward compatibility with data files that have less than the expected number of elements. Thanks again Dave Show quoteHide quote "Phill W." wrote: > > Dave Cullen wrote: > > I have a program that reads a delimited text file and splits the input > > string into string array elements: > > > > Dim Data(41) as String > > > > 'read semicolon delimited fields into string array > > Data = SrRead.ReadLine().Split(";".ToCharArray) > > > > I expect up to 40 elements to be read. But if the input data string has > > less than that and I try to access Data(40) I get a > > System.IndexOutOfRange exception. I thought that declaring the array > > size first would take care of that. > > > > I know I can GetLength on the array and know how big it is. But is there > > a way to statically set the array bounds so I don't need to know? > > It's Kludgey in the extreme, but you could force the array size after > you've loaded it, as in : > > Dim Data as String() _ > = SrRead.ReadLine().Split(";"c) > > ReDim Preserve Data(40) > > I've yet to find a ".Net" equivalent of ReDim Preserve ... > > HTH, > Phill W.
Why not use DAO?
How I can find out on which platform I am running (32/64 bits)? Make inherited property invisible mouse right click your favorite VB 2005 book? Windows Service to copy files to a Mapped Drive How to determine if e-mail was sent OK Add Active Directory Users to a Group on a Workstation schema.ini Pipe Delimited to Access Database change date |
|||||||||||||||||||||||