警告BC42104:变量'通过'在被赋值之前使用。在运行时可能会产生空引用异常。
这是我的代码:
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
答案 0 :(得分:-1)
正如错误所说,您没有初始化变量传递,在某些情况下,您最终可能会使用它。
确切地说,如果控件落在' catch '阻止,变量' 传递'将没有任何值,这意味着如果没有此变量具有任何值,则可能会达到If (pword = pass)
语句。
要修复错误,只需在初始化时为变量分配空值或空字符串。例如,使用以下语句:
Dim pass As String = "";