簡體   English   中英

如何在Visual Basic中使用while循環從StreamReader中讀取內容?

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

考慮:

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

While (line = readFile.ReadLine) IsNot Nothing

我是Visual Basic的新手。 每次我運行這段代碼都會給我這個錯誤:

“ IS”需要具有引用類型的操作數

我該如何解決這個問題?

您不能在VB中將分配用作表達式。 相反,您應該執行以下操作:

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

您的代碼中的while循環是C#特有的習慣用法。 看一下MSDN上的VB.NET等效示例:

StreamReader類

盡管Konamiman的回答非常好,但我不想重復自己 ,因此,更喜歡以下模式,以避免重復調用ReadLine()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM