StringReader: How to determinethe number of lines before using ReadLine

  • Thread starter Thread starter Howard Kaikow
  • Start date Start date
H

Howard Kaikow

How does one determine the number of lines before using ReadLine.
This would enable the ReDim to be out of the loop.

sReader = New StringReader(xxx.GetObjectText_)
If Not (sReader Is Nothing) Then
i = -1
Do Until sReader.Peek = -1
i = i + 1
ReDim Preserve yyy(i) ' yyy is typed as String
yyy(i) = sReader.ReadLine
Loop
End If
 
Chris said:
You could do a couple different things. One is to run through the file but
don't save the contents to get a count.

Do Until sReader.Peek = -1
i = i + 1
sReader.ReadLine
Loop

Depending what you are doing with the data after you could use an array
list, it allows you to grow an array as you need it.

Dim MyArray as new Arraylist
Do Until sReader.Peek = -1
i = i + 1
MyArray.Add(sReader.ReadLine)
Loop

You could also just read the file in all at once.

Dim S as String = sReader.ReadToEnd()

Hope it helps

Thanx.

I am using ReDim Preserve to grow the array as the lines are read, but that
is not very efficient.
I had hoped that there was a count property somewhere.

In this app, the arrays are rather small, so the overhead of ReDim Preserve
should not be noticeable.
For larger arrays, the choices seem to be to make a separate pass to count
the lines, or to grow the array with ArrayList. Not sure which would be
faster.

In any case, I do not want to use Arraylists instead of normal arrays,
unless I can see an advantage.
 
Howard Kaikow said:
Thanx.

I am using ReDim Preserve to grow the array as the lines are read, but
that
is not very efficient.
I had hoped that there was a count property somewhere.

In this app, the arrays are rather small, so the overhead of ReDim
Preserve
should not be noticeable.
For larger arrays, the choices seem to be to make a separate pass to count
the lines, or to grow the array with ArrayList. Not sure which would be
faster.

In any case, I do not want to use Arraylists instead of normal arrays,
unless I can see an advantage.

Redim Preserve for an smallish array of value types is really not that
expensive since the array only has a pointer for each member. The main
advantage of an ArrayList is that it will do fewer "redim"s. The
ArrayList's internal array will grow by 50% each time, or some such clever
ratio designed to strike a balance between wasted space and excessive
copying.

You coule do the same thing with

Do Until sReader.Peek = -1
i = i + 1
if i > UBound(yyy) then
ReDim Preserve yyy((i*3)\2)
end if
yyy(i) = sReader.ReadLine
Loop
ReDim Preserve yyy(i)



David
 
I would make a first guess at the size then check the no. of lines in the
loop and increment the dimension using preserve by 25 or 50 or whatever then
when done, do a final redim preserve to the no. of lines. Of course,
Arraylists are faster and you can always copy the arraylist into an array
when the loop is finished.

Chris said:
You could do a couple different things. One is to run through the file but
don't save the contents to get a count.

Do Until sReader.Peek = -1
i = i + 1
sReader.ReadLine
Loop

Depending what you are doing with the data after you could use an array
list, it allows you to grow an array as you need it.

Dim MyArray as new Arraylist
Do Until sReader.Peek = -1
i = i + 1
MyArray.Add(sReader.ReadLine)
Loop

You could also just read the file in all at once.

Dim S as String = sReader.ReadToEnd()

Hope it helps
 
You could do a couple different things. One is to run through the file but
don't save the contents to get a count.

Do Until sReader.Peek = -1
i = i + 1
sReader.ReadLine
Loop

Depending what you are doing with the data after you could use an array
list, it allows you to grow an array as you need it.

Dim MyArray as new Arraylist
Do Until sReader.Peek = -1
i = i + 1
MyArray.Add(sReader.ReadLine)
Loop

You could also just read the file in all at once.

Dim S as String = sReader.ReadToEnd()

Hope it helps
 
Howard,

Because I see where you need that value for in this messagethread, than is
my answer that I would just read it and Add the strings to an Arraylist.
An Arraylist is a collection of references to strings (or whatever) that are
build anyway when you are reading.

Just my thought,

Cor
 
Back
Top