Krešimir Čulina
Krešimir Čulina

Reputation: 93

warning BC42104 - Variable used before assigned a value

I'm trying to download a string from a file and I'm getting the following warning(s)

warning BC42104: Variable 'inst' is used before it has been assigned a value. A null reference exception could result at runtime.

This is my code

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

Upvotes: 1

Views: 11699

Answers (3)

Jerry Tang
Jerry Tang

Reputation: 1

Dim Employees() As Integer
ReDim Employees(6)
Dim rnd As new Random
For i = 0 To 5
    Employees(i) = rnd.Next(12, 400)
Next

yes add the new is ok

Upvotes: -1

Bobi
Bobi

Reputation: 1

I had a similar situation and I did the same as above but for a 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

Upvotes: -1

Guffa
Guffa

Reputation: 700342

The code would certainly cause a null reference exception. You have declared variables to hold the WebClient object, but you haven't created any actual WebClient instances.

Create instances of the WebClient class for the variables:

Dim inst As WebClient = New WebClient()

or the shorthand:

Dim inst As New WebClient()

Upvotes: 1

Related Questions