我在互联网上找到了一些代码如下(稍作修改)。
它只是请求网页的内容。
Private Sub readWebpage(ByVal url As String)
Dim Str As System.IO.Stream
Dim srRead As System.IO.StreamReader
Try
' make a Web request
Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
Dim resp As System.Net.WebResponse = req.GetResponse
Str = resp.GetResponseStream
srRead = New System.IO.StreamReader(Str)
' read all the text
textContent.text = srRead.ReadToEnd
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Unable to download content from: " & url)
Finally
srRead.Close()
Str.Close()
End Try
End Sub
但是我收到两个警告:
Warning 1 Variable 'srRead' is used before it has been assigned a value. A null reference exception could result at runtime.
Warning 2 Variable 'Str' is used before it has been assigned a value. A null reference exception could result at runtime.
我知道我可以简单地忘记Finally
并将代码添加到 try 块。
这是要走的路还是我可以使用不同的方法来防止警告?
提前感谢您启发我!:)