Problem reading text file

G

Guest

Hi everyone,

Do you know why the following code only read certain number of lines of text
file, but not the entire file?

Dim sr As StreamReader
Dim str As String
Dim al As ArrayList = New ArrayList
Do
str = sr.ReadLine
If str <> Nothing Then
al.Add(str)
End If
Loop Until str = Nothing

Thank you for your attention.

Nina
 
H

Herfried K. Wagner [MVP]

Nina said:
Do you know why the following code only read certain number of lines of
text
file, but not the entire file?

Dim sr As StreamReader
Dim str As String
Dim al As ArrayList = New ArrayList
Do
str = sr.ReadLine
If str <> Nothing Then

'If str Is Nothing Then'.
al.Add(str)
End If
Loop Until str = Nothing

'Loop Until str Is Nothing'.

BTW:

Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>
 
G

Guest

Thank you so much Mr. Wagner. I replaced "=" with "Is" as you suggested in
my code, and it worked. I'm curious why cannot use "=" here?

Nina
 
H

Herfried K. Wagner [MVP]

Nina said:
Thank you so much Mr. Wagner. I replaced "=" with "Is" as you suggested
in
my code, and it worked. I'm curious why cannot use "=" here?

'=' doesn't check for reference equality, it checks values only. 'If Foo =
Nothing Then...' is the same as 'If Foo = "" Then...'. 'Nothing = ""'
returns 'True', 'Nothing Is ""' returns false. If an empty line is read,
the string read from the file is "", not 'Nothing'. When the end of the
file is reached, the string read from the file is 'Nothing', not "".
 

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