警告BC42104 - 在分配值之前使用的变量

时间:2013-07-07 19:59:12

标签: vb.net

我正在尝试从文件中下载字符串,我收到以下警告

  

警告BC42104:变量'inst'在被分配之前使用   值。在运行时可能会产生空引用异常。

这是我的代码

Dim inst As WebClient
        Dim inst2 As WebClient
        Dim inst3 As WebClient
        Try
            MsgBox("started")
            ver = inst.DownloadString("http://www.xxxxxxxxx.com/update/version.xml")
            loc = inst2.DownloadString("http://www.xxxxxxxxx.com/update/loc.xml")
            desc = inst3.DownloadString("http://www.xxxxxxxxx.com/update/description.xml")
            If (String.Compare(ver, String.Format(Nothing, My.Application.Info.Version.Major.ToString) + "." + String.Format(Nothing, My.Application.Info.Version.Minor.ToString)) = False) Then
                updreq = True
            End If
        Catch ex As Exception
            MessageBox.Show("Error occured: " + ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

2 个答案:

答案 0 :(得分:1)

代码肯定会导致空引用异常。您已声明变量以保存WebClient对象,但您尚未创建任何实际的WebClient实例。

为变量创建WebClient类的实例:

Dim inst As WebClient = New WebClient()

或简写:

Dim inst As New WebClient()

答案 1 :(得分:-1)

我有类似的情况,我做的与上面相同,但对于TabPage:

Private Sub btnAddTab_Click(sender As Object, e As EventArgs) Handles btnAddTab.Click
    Dim number As Integer = TabControl1.TabPages.Count
    Dim tab As TabPage = New TabPage()
    tab.Text = "TabPage" & number + 1
    TabControl1.TabPages.Add(tab)
    tab.BackColor = Color.DarkGreen
End Sub