【问题标题】:prevent warnings on variables which aren't assigned a value in a Try防止警告未在 Try 中赋值的变量
【发布时间】:2010-12-22 21:32:07
【问题描述】:

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

它只是请求网页的内容。

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

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

提前感谢您启发我! :)

【问题讨论】:

    标签: .net vb.net try-catch try-catch-finally


    【解决方案1】:

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

    Dim Str As System.IO.Stream = Nothing
    

    【讨论】:

    • 这是我使用的方法,但我讨厌在 vb.net 中我不能执行以下操作 Dim a,b,c,d as double = Nothing 我必须执行以下操作 Dim a as Double = Nothing Dim b as Double = Nothing ... 这是更多代码行只是为了关闭警告。
    • 这看起来很荒谬。我讨厌 VB.NET。你会认为Dim Str As System.IO.Stream 是等价的。
    【解决方案2】:

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

    处理这种情况的一种方法是使用 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
    

    【讨论】:

    • 使用Using时不用关闭连接?
    • 不,Using 语句会为你做这件事。 (Close 和 Dispose 一样)
    • 太棒了。我认为这是最好的方法。感谢您提供所有信息!
    • @Conrad 你不需要额外的 Nothing 检查,默认情况下编译器将对象设置为 Nothing ,所以 "Dim str As Stream" 和 "Dim str As Stream = Nothing" 应该产生完全相同IL代码,完全没有区别。但无论如何使用“使用”总是尽可能推荐的方法。
    • @Dr.邪恶的。我讨厌不同意,但如果 Str = resp.GetResponseStream 抛出异常 srRead 将保持无。在 finally 块中,srRead.Close 将导致 NullReferenceException。我很确定有人会想避免这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多