防止在 Try 中未分配值的变量出现警告

发布于 2024-10-09 00:25:56 字数 1134 浏览 7 评论 0原文

我在互联网上找到了一些代码如下(稍作修改)。

它只是请求网页的内容。

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 块中。

这是可行的方法还是我可以使用不同的方法来防止警告?

预先感谢您启发我! :)

I found some code on the internet as below (slightly modified).

It simply requests the content of a webpage.

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

However I get two warnings:

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.

I know I can simply forget about the Finally and add the code to the try block.

Will that be the way to go or can I prevent the warnings using a different approach?

Thanks in advance for enlightening me! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2024-10-16 00:25:56

默认情况下,您可以简单地将它们设置为 Nothing,这样您就可以通知编译器您知道自己在做什么:)

Dim Str As System.IO.Stream = Nothing

You can simply set them Nothing by default, this way you'll inform the compiler you know what you are doing :)

Dim Str As System.IO.Stream = Nothing
聚集的泪 2024-10-16 00:25:56

该警告是因为如果 GetResponseStream 出现错误,那么您的 srRead 将为 null,从而导致 Null 异常。

处理这个问题的一种方法是使用Using,它会自动处理这些对象

 Private Sub readWebpage(ByVal url As String)
        Try
            ' make a Web request
            Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
            Dim resp As System.Net.WebResponse = req.GetResponse
            Using Str As System.IO.Stream = resp.GetResponseStream
                Using srRead As System.IO.StreamReader = New System.IO.StreamReader(Str)
                    textContent = srRead.ReadToEnd
                End Using
            End Using


    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Unable to download content from: " & url)

    End Try
  End Sub

你也可以按照Dr. Evil建议的方式将对象设置为Nothing而不是Using关键字,然后你会希望在你的finally中使用它

Finally 
        If Str Is Not Nothing Then
            Str.Close
        End If
        If srRead Is Not Nothing Then
            srRead.Close
        End If
End Try

The warning is because if there's an error on GetResponseStream then your srRead will be null resulting in a Null Exception.

One way to handle this is use Using which will automatically dispose of these objects

 Private Sub readWebpage(ByVal url As String)
        Try
            ' make a Web request
            Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
            Dim resp As System.Net.WebResponse = req.GetResponse
            Using Str As System.IO.Stream = resp.GetResponseStream
                Using srRead As System.IO.StreamReader = New System.IO.StreamReader(Str)
                    textContent = srRead.ReadToEnd
                End Using
            End Using


    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Unable to download content from: " & url)

    End Try
  End Sub

You could also go the way Dr. Evil suggests setting the object to Nothing instead of the Using keyword then you'll want this in your finally

Finally 
        If Str Is Not Nothing Then
            Str.Close
        End If
        If srRead Is Not Nothing Then
            srRead.Close
        End If
End Try
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文