Problem reading text file

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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>
 
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
 
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 "".
 
Back
Top