开发者

How do I read from a StreamReader with a while loop in Visual Basic?

Consider:开发者_StackOverflow社区

Dim line As String
Using readFile As New StreamReader(SalesUpdateFile)

While (line = readFile.ReadLine) IsNot Nothing

I am new to Visual Basic. And every time I run this code it gives me this error:

"IS" requires an operand that have a reference type

How can I fix this problem?


While Konamiman's answer is perfectly fine, I don't like to repeat myself and, thus, prefer the following pattern to avoid duplicating the call to ReadLine():

Do
    Dim line = reader.ReadLine()
    If line Is Nothing Then Exit Do
    ' Process the line
Loop


You can't use an assignment as an expression in VB. Instead you should do something similar to this:

line = readFile.ReadLine
While (line IsNot Nothing)
    'process the line
     line = readFile.ReadLine
End While


The while loop you have in your code is an idiom specific to C#. Take a look at this example on MSDN for the VB.NET equivalent:

StreamReader Class


Do
     Dim line = reader.ReadLine()           
     ' Process the line
Loop until line is Nothing
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜