BC42104 错误代码 我该如何解决这个错误?

BC42104 error code how can I fix this error?

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

这是我的代码:

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    Dim uname As String = ""
    Dim pword As String
    Dim username As String = ""
    Dim pass As String
    If TextBox1.Text = "" Or TextBox2.Text = "" Then
        MsgBox("Please fill the info")
    Else
        uname = TextBox1.Text
        pword = TextBox2.Text
        Dim query As String = "Select Password From Register where Username= '" & uname & "';"
        Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gui\Documents\Database4.accdb"
        Dim conn = New OleDbConnection(dbsource)
        Dim cmd As New OleDbCommand(query, conn)
        conn.Open()
        Try
            pass = cmd.ExecuteScalar().ToString
        Catch ex As Exception
            MsgBox("Username does not exit")
        End Try
        If (pword = pass) Then
            MsgBox("Login success")
            Reg.Show()
            If Reg.Visible Then
                Me.Hide()
            End If

        Else
            MsgBox("login Failed")
            TextBox1.Clear()
            TextBox2.Clear()
        End If
    End If
End Sub

如错误所述,您没有初始化变量 pass,在某些情况下,您最终可能会使用它。

确切地说,如果控件位于“catch”块中,则变量“pass”将没有任何值,这意味着可能在没有此变量具有任何值的情况下达到 If (pword = pass) 语句。

要修复错误,只需在初始化时为变量分配一个空值或空字符串。例如使用这个语句:

Dim pass As String = "";