overriding array size

D

Dave Cullen

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
 
V

vbnetdev

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)
 
H

Herfried K. Wagner [MVP]

Dave Cullen said:
I have a program that reads a delimited text file and splits the input
string into string array elements:

Dim Data(41) as String

.... is semantically equivalent to 'Dim Data() As String = New String(41)
{}'.
'read semicolon delimited fields into string array
Data = SrRead.ReadLine().Split(";".ToCharArray)

'Split' returns a dimensioned and filled array object. Thus you do not need
to dimension the array at its declaration:

\\\
Dim Data() As String = SrRead.ReadLine.Split(...)
///
 
M

Master Programmer

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
 
P

Phill W.

Dave said:
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.
 
D

Dave Cullen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top